Implementing static files cache in asp.net mvc - asp.net

I am trying to implement static files cache in the ASP.net mvc application.
What i did:
I've added into Content folder a web.config file with the following content:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="300.0:00:00" />
</staticContent>
</system.webServer>
</configuration>
In the website web.config file, i commented out some lines of code:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<!-- Commented them out
<add verb="GET" path="*.js" name="Static for js" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.css" name="Static for css" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.png" name="Static for png" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.jpg" name="Static for jpg" type="System.Web.StaticFileHandler" />
-->
</handlers>
Now, publishing the website and inspecting the resources, i get the folowing response:
I see is missing the Expire header ?! (shouldn't it be there in order for cache to work)
Does the response headers tell the browser to cache the resource for the next 25920000 seconds?
I am doing things correctly in order to cache the static files?

This is correct, and you should be fine.
Some might say you should add Expires, too, for clients which do not understand HTTP/1.1, but as already argued in the first linked article, that shouldn't be a real concern, even less now, 7 years later.

Related

HTTP Patch Verb not working on windows server 2012 r2

I'm working on single page application which consumes WebApi for data manipulation. My site was hosted on Windows Server 2008 R2 (IIS 7.5.7600.16385) and working fine, however after migration to Windows Server 2012 R2 (IIS 8.5.9600.16384) HTTP PATCH VERBS are not working, api call returns with status code 400 "Bad Request". I have tried following code snippet in web.config but got no luck.
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" type="System.Web.Handlers.TransferRequestHandler" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
<add name="Telerik.ReportViewer.axd_*" type="Telerik.ReportViewer.WebForms.HttpHandler, Telerik.ReportViewer.WebForms, Version=9.0.15.225, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" path="Telerik.ReportViewer.axd" verb="*" />
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
Googling again and again but got nothing useful, I'm stuck here, Any help would be appreciated.
These are the web.config settings I used when I had a similar problem with PUT requests after moving servers.
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
When I tried to send API call outside from network, everything was working as expected. Further investigations reveals that network firewall policy was problematic. By mistake production server was blacklisted by NOC team.
Thanks all for your efforts :)

Custom IHttpHandler mapping in ASP.NET MVC

I am trying to configure a custom IHttpHandler in web.config and I don't understand the behavior I'm getting. I'm trying this using IIS express.
This is how my Sytem.WebServer section of web.config looks like:
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="dynamicPngHandler" verb="*" path="*.png" type="ImagServingTest.HttpHandler.PureCustomPngHandler, ImagServingTest"/>
</handlers>
</system.webServer>
http ://localhost:48705/default.png (does not work, I get a 404)
http ://localhost:48705/something/default.png (does not work, I get a 404)
http ://localhost:48705/something/something/default.png (does not work, I get a 404)
http ://localhost:48705/something/something/something/default.png (works)
I don't understand why this is happening. Why does the mapping fail until I have a url with three parts after localhost?
I imagine the fact that this is an MVC4 app is important (although I can't see why).
UPDATE
I've tried this using web forms instead of MVC and it works as expected.
In the MVC project I tried moving the custom handler so it was the first on the handlers' list in web.config and it still did not work.
The reason for the request failing for those urls is that they were being covered by MVC's default routing rule: "{controller}/{action}/{id}" because all those have defaults.
I've added routes.IgnoreRoute("{ignore}.png"); to RouteConfig and I'm now seeing the behavior that I expected

Can't make ASP.NET Website cache static files

I am trying to make Asp.net website to cache static files to the browser client.
I followed these steps
My web config:
<configuration>
<!-- elements removed for readability -->
<location path="content">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="100:00:00" />
</staticContent>
</system.webServer>
</location>
</configuration>
However, requesting a js file from the Content folder does not cache the file:
What i am doing wrong? I really need to fix this problem.
Removing System.Web.StaticFileHandler configurations from web.config fixed the issue, but i don't know if this is the correct way to solve the problem:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<!-- Removing these rules makes the cache to work.
With them, it doesn't work
<add verb="GET" path="*.js" name="Static for js" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.css" name="Static for css" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.png" name="Static for png" type="System.Web.StaticFileHandler" />
<add verb="GET" path="*.jpg" name="Static for jpg" type="System.Web.StaticFileHandler" />
-->
</handlers>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="100:00:00" />
</staticContent>
</system.webServer>

IIS 8 - Default Document - resource cannot be found

I'm trying to understand why my default document doesn't come up when I browse the virtual directory. If I browse to the site like I should be able to, I get this:
However, if I add the page to the URL, it comes up:
One SO answer suggested removing all of the default documents (in IIS) except the real one. I tried that (image below) but it didn't help.
Why won't IIS serve that page when using the root URL (http://localhost/SignalRChat)?
This is the relevant part of the web.config after removing the default docs:
<defaultDocument>
<files>
<remove value="default.aspx" />
<remove value="iisstart.htm" />
<remove value="index.html" />
<remove value="index.htm" />
<remove value="Default.asp" />
<remove value="Default.htm" />
<add value="ChatPage.cshtml" />
</files>
</defaultDocument>
This is the handlers section:
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Another possible reason of the 404 error when your request doesn't point to a specific file name at the end of URL is that your IIS Request Filtering rules deny extensionless requests.
To know it for sure, figure out the subcode of the 404 error.
Usually, it's 404.7, but there may be some variations,
Also, you can try to allow extensionless requests explicitly by adding <add fileExtension="." allowed="true" /> to your web.config as below:
<system.webServer>
<security>
<requestFiltering>
<fileExtensions>
<add fileExtension="." allowed="true" />
</fileExtensions>
</requestFiltering>
</security>
</system.webServer>
Please let me kniw if it helped or post your exact 404 error with the subcode.
From your tags it looks like you use MVC and a view using the razor view engine (cshtml). In MVC an URL does not map to a document directly. So the discussion should not be about default documents, handlers and IIS configuration.
An URL must match a defined route, which invokes an action on a controller. This action will then render the view (*.cshtml).
Try to fix your routes to be able to handle the request. If you need more help, you should update your question with more information about the controller and your routes.
You can made a web Site/Application installer. it has option to set the start up page selection.
So set the ChatPage.cshtml as your start up page in installer and after installing if you type localhost/SignalIRChat
you will see the content of ChatPage.cshtml without showing the "ChatPage.cshtml" name in address bar.
It will resolve your problem.
You need to add this Handler at the end of your handlers section:
<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" requireAccess="Read" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" />
Update:
Since .cshtml files are blocked by default, you need:
<handlers>
<remove name="cshtml-ISAPI-4.0_64bit" />
<remove name="cshtml-ISAPI-4.0_32bit" />
<remove name="cshtml-Integrated-4.0" />
<remove name="cshtm-ISAPI-4.0_64bit" />
<remove name="cshtm-ISAPI-4.0_32bit" />
<remove name="cshtm-Integrated-4.0" />
</handlers>
<staticContent>
<mimeMap fileExtension=".cshtml" mimeType="text/html" />
</staticContent>
Remove the StaticFile handler suggested (if still there).
This could be an MVC issue.
You may need a redirection from the index to this other action in your SignalRChat controller.Something like this:
public ViewResult Index()
{
return View("ChatPage");
}
It seems your directory browsing is disabled in IIS. Just enabled the directory browsing in your IIS. it will solve your problem.
visit http://technet.microsoft.com/en-us/library/cc731109%28v=ws.10%29.aspx for more details.
Hope this will help.

How to ASP.NET MVC3 Route to accept any type of file (Dot in URL)

ASP.NET MVC 2 Issue - Dot in Route gives an example to allow dot (.) in url and allow .svc file.
My question is, how to allow any kind of file, I tried:
<httpRuntime relaxedUrlToFileSystemMapping="true" />
<remove extension=".*"/>
Not working! What I want is: for any kind of URL (any chars) can processed by MVC3 Route(MapRoute).
That's a very very very bad idea. You want people to be able to download your web.config? see your passwords? download your database? IIS and MVC deliberately block many kinds of extensions because not doing so is highly insecure.
here is the solution:
First: it will NOT cause security leak, if you visit /web.config, you will get
HTTP Error 404.8 - Not Found
The request filtering module is configured to deny a path in the URL that contains a hiddenSegment section.
Unless you change the hiddenSegment of course.
here is the solution, at web.config file:
<system.webServer>
<handlers>
<add name="Static-Favicon" path="favicon.ico" verb="GET" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="Static-Robot" path="robots.txt" verb="GET" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Static-Favicon and Static-Robot can be removed, if you like your MVC handle these two files as well.
Btw, you still need the <remove extension=".svc" /> to handle svc file, and relaxedUrlToFileSystemMapping for special codes in url

Resources