CacheDuration in WebMethod ignored - c#

I try to cache a webmethod of a webservice. By following the documentation, I have tried to add the CacheDuration attribute, and made a test :
[WebMethod(CacheDuration = 180)]
public int TestCacheDuration()
{
return new Random().Next();
}
For each call of the webmethod, I have a different response, so it is not cached.
Is it normal ?
Thanks for your answers!

This is clearly explained in MSDN forums:
There are two issues that can affect output caching in an ASP.NET 2.0
Web service application.
In ASP.NET 2.0 the HTTP method of the test page has changed from GET
to POST. However, POSTs are not normally cached. If you change the
test page in an ASP.NET 2.0 Web service application to use GET,
caching works properly.
In addition, HTTP indicates that a user agent (the browser or calling
application) should be able to override server caching by setting the
"Cache-Control" to "no-cache". ASP.NET applications, therefore, ignore
cached results when they find a "no-cache" header.
and also:
Use protocols element under webSerices element in web.config:
http://msdn2.microsoft.com/en-us/library/ms228319(VS.85).aspx
source: http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/3765f1e2-0ff5-4840-afa2-e85b3d909cd1

Related

Asp.Net Core: Developer Exception Page for Cross-Site Requests

In our project we have an ASP.Net Core Web API backend and an Angular 5 frontend. In development mode, the Angular Page will run on its own server (using node, e.g. localhost:8000). Therefore, accessing a local instance of the API (e.g. localhost:57612) will be a cross-origin request.
CORS is correctly set up for the API, and in the successful case there are no issues. Also, I have enabled app.UseDeveloperExceptionPage() in the API to get nice stack traces in case something goes wrong.
However, Asp.Net only returns the error details/stack trace (as HTML in the error response body) when the request was sent from the same origin (i.e. localhost:57612 in the example above). So in our development setup, we don't get this error response body, we get nothing.
I have not found a way to send the developer error page as response to an errored cross-orign request. In Asp.Net 4.5 + IIS, there was a setting in IIS where one could control this behavior specifically. Does anybody know how to control this behavior in Asp.Net core?
(I think I could implement a workaround in form of a custom ASP.Net error handler that will return all the details of the exception like the Developer Exception Page. But that's a bit silly, reimplementing this functionality.)

How to consume web API service from a webforms application?

I have a webforms application in a big solution folder with multiple projects. I wanted to consume a web api application which is also a part of the solution. So my client looks like as follows.
function GetText()
{
$.getJSON("api/SiteUsers",
function (data) {
$("#TestText").append(data);
});
}
and the controller is a simple string returning action.
[HttpGet]
public string TestText()
{
return "this is a text";
}
when I try to call the service I get a 404 error the following link could not be found
http://localhost:1234/MyAspxProj/MyFolder/MyPage/api/SiteUsers
I can understand it is probably because it is trying to find the resource from within the webforms application. How can I call the web api service? I am open to all suggestions and advice.
lets say your API is hosted on localhost:1111 and your webforms application is hosted on localhost:2222
first make sure you can get the results of the API you just created by going to:
localhost:1111/api/SiteUsers
once you are sure that the above URL is returning what you expect, you can be sure that your API is set-up correctly.
Now lets come to the next issue, accessing API from another application (i.e. not having the same Host as the API i.e. localhost2222)
To access APIs from an application that is on another domain, you need to enable CORS support on the WebAPI. There are manay resources on the internet that will explain you how you can achieve this: google for enabling cors in web api 2
Once you have set-up CORS on your web api project, you will be able to access your API from any application.
Remember: you only need to enable CORS if the client is on different domain AND the client is a web based client (which in your case it is i.e. web forms application)
I hope this will give you some direction.
Based on the info you've provided, it looks to me like you're not putting in the correct url. Assuming your [HttpGet] function is within a file at "api/SiteUsers", you would use a url like this: "api/SiteUsers/TestText"

Authorization through a ASP.NET Web API in Orchard CMS

Creating an ASP.NET Web API module in Orchard CMS is simple and straightforward. The following link explains how to do it and it works just fine. http://www.sebastienros.com/web-api-makes-it-in-orchard
However, the GET requests does not work when the WebAPI is running under Orchard and you use the [Authorize] attribute at the same time.
[Authorize]
public IEnumerable<string> Get()
{
return _moduleManager.GetUsers().Select(n => n.UserName);
}
When I call this from the client
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("user", "password");
HttpClient client = new HttpClient(handler);
var response = await client.GetAsync("http://localhost:30321/OrchardLocal/api/MyWebAPIModule/Users");
Console.WriteLine(response);
the response variable returns to me the "Not found" HTML page from Orchard. Without the [Authorize], it returns a list of users.
Does Orchard have something already built-in to match the credentials with a registered user in Orchard? Or is there steps missing in the process?
This blog post may be a helpful resource for a deeper understand ASP.NET's Authorize attribute. It might help to look in the web.config file to see what the authentication mode is set to.
I think the problem is that if you are making a call in code, you need to pass any cookies in the request.
A user is authenticated against the website by the use of the aspnetAuth (or FedAuth) cookie, which is provided by the browser. So if you called /OrchardLocal/api/MyWebAPIModule/Users from the browser you would expect this to work (you should see this happen in fiddler by looking at the headers/cookies).
However if you make a call in code you need to pass cookies/auth. header yourself. The call you have does not have any of this, thus it fails (you should see the absence of the cookie in fiddler for this request).
I'm not sure why you would call the api in this way from within your own module. Presumably the API controller calls a service that does the actual workload. You could call this service directly from your Driver/Action, still safe in the knowledge that your business logic is behind the service interface.

How to set custom cache response headers for virtual paths?

We have a virtual URL, /bundles. We want to be able to check, at some point during the .NET lifecycle, if the url starts with /bundles and then set headers. We've thought about using HttpCachePolicy Class and using setCacheability and setMaxAge. I'm wondering how we can apply that to any file served via the /bundles route? Where is the best place to handle this?
Sounds like you want a "different" caching behavior for this Route.
I assume that you have a special Controller for this Route.
If so then you could use the OutputCache Attribute on your Action Methods inside the Controller.
[OutputCache(Duration=[InSeconds], ...)]
public ActionResult YourMethod()
{
...
}
this will result in using the ASP.NET Cache Framework.
Optional: You could use a Profile which you set in the IIS WebSite Konfiguration, then you would have to use the Attribute with the Profile Parameter.
[OutputCache(Profile="YOUR_PROFILE")
IIS will add the associated Response Headers like Expire / Cache-Control / Last-Modified...
Also you would gain the Output Cache Feature which is a performance boost.
But if you want to get "full" Control over your Response Headers then you have to create an own IIS Handler where you override the Output Methods.
Because if you have an enabled Dynamic Compression, then the IIS will remove all Response Headers during ASP.NET lifecyle and add "needed" Response Headers after Compression which happens after ASP.NET processes.
Somewhere on the MSDN is a visualization of the IIS Cache Tiers. But you have to make a "deep" search on the MSDN. I would give you a link but that will take a longer time.. ;)

Request to a IIS server (when ASP NET WebForms + MVC co-exist)

Well, there are many questions on SO which proves that you have the possibility to hold both ASP WebForms as well as ASP NET MVC Applications running together.
Note:
In MVC, What happens ? The short answer is Every requests to the webserver first goes through aURLRoutingModule object which reads the request and performs route-selection (depending upon global.asax file). The MvcRouteHandler then handles the controller method invocation.
That is okay.
All I am trying to understand is what happens when a user sends a request to the browser when the web server contains both Web-Forms as well as an ASP.NET MVC Application.
Who reads the URL and understands whether it is an .aspx extension or it is a request to mvc ?
How exactly the ISAPI.dll function in this case?
It's actually fairly simple: the URLRoutingModule checks the disk to see if there is a physical file which matches the current request URL. If there is a match, the Routing module assumes that it shouldn't run for this request, and ASP.NET's default behavior is then to look up and invoke the default handler for this extension. In the case of .aspx, that would be the Web Forms handler.
You can change this via the RouteCollection.RouteExistingFiles property in your RegisterRoutes method. This property controls whether the Routing module should intercept the request regardless of whether there is a physical file on the hard drive.
To understand better what happens when a user sends a request to the browser when the web server contains both Web-Forms as well as an ASP.NET MVC Application is by knowing on how ASP.Net runtime perform the Url routing which it is actually done by the ISAPI filter plugin.
Below are the detailed steps for Webform request:
1) When user enter a url like website1.com/page1.aspx in the browser.
2) Browser will send a Get Request to the IIS server asking for Page1.aspx file.
3) Then ISAPI filter is the first process to access the Http data before IIS can start to do anything. Here it act like a router for IIS request.
4) ISAPI filter will then check the url whether the filename has any extension (.aspx) and check whether the (.aspx) extension is mapped to the aspnet_isapi.dll or not.
5) If ".aspx" is mapped, it will invoke the ASP.net UrlRoutingModule and mapped the HttpHandler for ASP.net files.
For MVC request:
1) It follow the same step like webform request until the ISAPI filter found that there is no filename and file extension exist in the Url since MVC Url only contain the controller and action name.
2) All Url need to be mapped to a handler when the request initially received because routing will not work with unmanaged handler request
3) To direct this unmanaged request to managed code, it can be done by setting wildcard mapping to aspnet_isapi.dll for IIS6. For IIS7, we can just set the runAllManagedModulesForAllRequest="True".
4) Then only ASP.Net UrlRoutingModule will be invoked and try to match the Url with the application defined route.
5) If there is a match, it will direct to HttpHandler of Asp.net MVC framework. But if there is no match, the default Asp.net webform mapping will be used and most likely the request will return a HTTP 404 error result.
For more details you may refer to this web:
Mixing Web Forms and ASP.NET MVC
Web Server and ASP.NET Application Life Cycle in Depth

Categories

Resources