enable download on html from my asp page - c#

I want to be able to download the html contents of my page from a client after receiving a post request (XMLHttpRequest and doing some processing. Is there a setting I must enable for this to be possible? I've tried enabling COORS via
Response.AddHeader("Access-Control-Allow-Origin", "*");
with no success. Has anyone found a solution to this problem?
Update: I am using ASP.NET Web Forms (with NET 4.5)

Well, I think the CORS stuff is for cross origin scripting - to enable JavaScript calls to a domain different than the original request. As far as getting the HTML contents, I believe you mean to get the response of the web server as it is posted back to the client. Not knowing what technology you have used (i.e., MVC, ASPX, etc.) you may want to look into interceptors. They differ depending on the technology. For ASPX, check out....
http://www.codeproject.com/Articles/30907/The-Two-Interceptors-HttpModule-and-HttpHandlers

Related

How to receive a simple POST in ASP.NET Core?

I "just" need to do a simple https server in ASP.NET Core in order to receive some XML streams through a POST verb.
I just have to write these XML on hard disk.
I don't really know where to begin to just running an ASP.NET Core server to get the POST data.
Anyone could give me some inputs or a sample code ? I don't need MVVM or something else, just getting POST data.
Thanks :)
You can get a starter app by running the command
dotnet new webapi
That will give you all the code needed to set up a server that can accept REST API calls.
If you do not want to use the MVC model in ASP.NET Core, you can use the Razor Pages model. Please refer to this documentation https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio. For Post Form data you will have to write a OnPostAsync method in you Razor Page.

How to use HTTPContext to get a web address in C#

I have been looking for a way to get the web address of websites currently open in a browser. I have come across lots of posts saying to use HttpContext.Current.Request.Url.AbsoluteUri or similar.
However from what I have read, HttpContext should be used once a http request has been made. How would I detect when this has happened since currently it always returns a null?
Or is there another way to do get web addresses currently being used?
Edit:
I am using Windows Forms. The overall aim is to get web address of pages the user has accessed from a form.
Thanks
Do you want to get the web address of a website currently open in the user's browser?
If so, I don't think the mentioned method will work.
Or do you want to get the web address of a page that is part of your application and is being accessed by the user?
If so, you can access this information in your controller method. The URI information is available at Request.RequestedUri. I believe this is valid for both WebApi controllers and MVC Controllers.
Also, I'm assuming you are working with ASP.NET Core. Please tell if using .NET Framework.

call a page within subdomain using ajax

I need to read a page content on my sub-domain using ajax get.
I thought of creating some script on my sub-domain and calling that script on my domain. however I don't know how to do it.
Is it possible to do such a thing? how?
EDIT:
I created a page in my domain that uses DownloadString and downloads that page and write down the result. any better ideas?
If you wish to support cross-domain AJAX requests, you will need to create a cross origin resource sharing handler (CORS). I don't have an example specific to asp.net, but if you are using MVC this is fairly simply to achieve (and would be fundamentally the same for WebForms).
A Web API example may be found here to get you started:
CorsHandler.cs
Also, if you need to support IE 7, 8 or 9 you would want to look in to a jQuery extension lib such as jQuery-ajaxTransport-XDomainRequest
EDIT
If you would like to look at a full example (again Web API unfortunately), here is a MSDN article with a full code example Implementing CORS support in ASP.NET Web APIs
You can also sometimes manipulate document.domain in JS. So if you're serving pages from www.site.com, and your Ajax services are hosted at app.site.com, set your document.domain to site.com.

Referencing jQuery CDN on a HTTPS secured page?

What's the preferred way of referencing a jQuery library on pages that use https? We have a checkout masterpage that needs to make use of jQuery UI widget but I keep getting an error:
Uncaught TypeError: Object #<Object> has no method 'dialog'
When calling the jQuery dialog. I've looked in chrome tools and the page is trying to get the library over https but it's location is defined as http in my markup. So what I'm asking is, how can i reference these libraries on secured pages? Do i reference a local copy within the site itself rather than a CDN? Or is there a https version that i can use?
Thanks
Google and Microsoft CDNs serve jQuery and jQuery UI through HTTPS as well. Just switch the protocol. E.g.:
https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
You can also just omit the protocol when referencing the external library:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
This way the script will be loaded using the same protocol as its document.
Both serve through HTTPS also, but the best way is to just not use HTTP part at all and use // only.
Example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
By not using any HTTP protocol in your code, it means you need not worry about the conditional logic for whether it is actually needed or not. So your shopping pages won't have to be targeted specifically.
Hope that helps clear it up.
Thanks.

http meta refresh issue

I am using http meta refresh to reflesh current page to keep session live. I am using VSTS 2008 + C# + .Net 3.5 and developing ASP.Net application.
My question is, is it possible to send from client (browser) side If-Not-Modified-Since request header to server side and check if at server side responses 304 Not-Modified header to client? (I want to use this to optmize bandwidth and overhead at server.)
If yes, could anyone recommend me some sample code at client side and at server side how to implement this?
This could potentially be addressed with the HtmlMeta class that was added in .NET 2.0. It allows programmatic access to the meta keywords. You can check it out here.
I'm not super familiar with ASP.NET, but in general, for something like this, consider using ETags.
You can use jQuery's Ajax api see the documentation here, there is a ifModified param. Moreover, the use of HTTP Meta Refresh is discouraged by W3C
You shouldn't use META Refresh. If you want to reload the page, use the JavaScript method window.location.reload(false);
I explain why here:
http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/meta-refresh-causes-additional-http-requests.aspx
and
http://blogs.msdn.com/b/ieinternals/archive/2010/07/08/technical-information-about-conditional-http-requests-and-the-refresh-button.aspx

Categories