Reducing the number of HTTP requests for cached resources - asp.net-mvc

I have an ASP.NET MVC 4 application running in IIS 8.5 / Windows 8.1.
I enabled caching for pretty much everything.
I used YSlow plugin for Google Chrome to see the number of HTTP requests for Primed Cache.
I managed to reduce HTTP requests for javascript and css files, but don't know how to do the same for .png and .ico files.
I tried to add this section in web.config under system.webserver
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
<!-- Image types -->
<remove fileExtension=".jpg" />
<mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
<remove fileExtension=".png" />
<mimeMap fileExtension=".png" mimeType="image/png" />
...
But that didn't work.
Any help would be greatly appreciated!

I've increased the cacheControlMaxAge value from 1.00:00:00 to 180.00:00:00 and that did the trick.

Related

IIS Express - HTTP Error 500.19 0x800700b7 on 'mimeMap' for 'fileExtension' set to '.svg'

I was adding the ability to serve SVG & WOFF files to my Azure hosted web application. I found the following instructions:
https://blogs.iis.net/richma/archive/2014/01/07/adding-mime-types-to-your-windows-azure-web-site.aspx
Which I have followed by adding the below text to my Web.config file:
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
</staticContent>
This works when published to Azure. However, now I get the following error from IIS-Express whenever I attempt to start up the website in Debug mode locally for every resource the website attempts to serve.
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Detailed Error Information:
Module: CustomErrorModule
Notification SendResponse
Handler StaticFile
Error Code 0x800700b7
Config Error Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.svg'
Config File snip\web.config
Requested URL http://snip:14466/resources/img/snip.png
Physical Path C:*snip*\Resources\img*snip*.png
Logon Method Anonymous
Logon User Anonymous
Config Source:
58: <mimeMap fileExtension=".json" mimeType="application/json" />
59: <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
60: <mimeMap fileExtension=".woff" mimeType="application/x-font-woff"/>
It clearly hates my mimeMap for .svg but this works fine out in Azure Websites. Is there something I am missing for locally running these changes to the Web.config?
And of course I find this answer after already posting my question:
Adding <mimeMap> entry Causes 500 for Other Static Content on IIS Express
So the issue is that my local IIS configuration has the modules already loaded, meanwhile the production environment doesn't. So to fix the issue for both environments you need to make sure the mimeMaps are unloaded before loading them.
The code is the following for the fix:
<staticContent>
<remove fileExtension=".json"/>
<mimeMap fileExtension=".json" mimeType="application/json" />
<remove fileExtension=".svg"/>
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
<remove fileExtension=".woff"/>
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff"/>
</staticContent>
Hope this helps others from wasting time looking for a fix.
I found that if you have any mimeMap items duplicated between your Web.Config in the directory hosting your app and the default %UserProfile%\Documents\IISExpress\config\applicationhost.config it will fail for them all.
Same solution worked: made sure to <remove> each <mimeMap> entry.

IIS hijacks CORS Preflight OPTIONS request

I am making a CORS POST request and setting the Content-Type header to json. This triggers a Preflight OPTIONS request to fire (this is good and expected)
This OPTIONS request is responded to with a 200 OK but this isn't coming from my WebAPI application.
I have a custom Message Handler in place and it never get's hit so the request is getting responded to by IIS prior to hitting ASP.NET it seems.
I have found several posts on the subject and they say the following
Make sure WebDav is uninstalled / removed / disabled - DONE
Make sure the OPTIONSVerbHandler is removed / changed to use aspnet_isapi.dll - TRIED BOTH
Make sure the extensionlessURLHandler includes the OPTIONS verb - DONE
However, my options request is still getting hijacked. By that I mean, IIS responds with at 200 OK but isn't including an Access-Control-Allow-Origin header in the response. It isn't including this header because it is never getting to my WebAPI CORS code that would set this header.
The two best posts I could find that sound like my issue are
here: JQuery stuck at CORS preflight and IIS ghost response
and here: http://brockallen.com/2012/10/18/cors-iis-and-webdav/
I have tried turning on Failed Request tracing (FERB) in IIS and set it to trace all 200 status codes. I don't ever see the options request being logged... Not sure if this means FERB doesn't track OPTIONS requests or if I need to change something in the FERB settings to make it track OPTIONS requests, Or if this is a clue to what my problem is?
This is ASP.NET WebAPI 2.0 running on IIS 7.5 (Also tested on IIS 8 and IISExpress with same results)
Doesn't matter what browser (Chrome, FF, and IE all fail the same way)
I have tried everything I can find on the subject and still can't fix my problem.
Help me StackOverflow, you're my only hope.
A couple of things you can try here, all web.config related, firstly modify your modules element to include the attribute runAllManagedModulesForAllRequests="true", as below:
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDavModule" />
</modules>
Then set your handlers to the below:
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="WebDav" />
<remove name="OPTIONSVerbHandler" />
<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>
This should do the trick, but if it doesn't, as a last resort you can force IIS to output the correct headers with the below:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
Be wary of the wildcard value, you should really set this to the domain name that your site will be hosted on.
that's what worked for me after 4 hours of searching/experimenting:
<handlers>
<remove name="OPTIONSVerbHandler" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="C:\Windows\System32\inetsrv\asp.dll" resourceType="Unspecified" requireAccess="None" />
</handlers>
I tried all of the above suggestions as well as others I found on SO and what mattered in my situation was we had Request Filtering enabled on IIS and the OPTIONS HTTP Verb was not in the list of allowed verbs. Once I added it I was able to sort out the rest of it.
I had the same issue and the following web.config settings fixed it for me.
<modules runAllManagedModulesForAllRequests="false">
<remove name="FormsAuthenticationModule" />
</modules>
<handlers>
<remove name="OPTIONSVerbHandler" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
I was then able to handle CORS OPTIONS requests manually in Application_BeginRequest.
I was originally using the library detailed in this blog post for handling CORS requests. The product I'm working on requires that runAllManagedModulesForAllRequests be set to false, though. This is why I had to set up a custom implementation, but if you don't have that requirement you should give that library a try. It worked great when I was able to have runAllManagedModulesForAllRequests set to true.
In our case it was request filtering in IIS disabling OPTIONS verb at the root web application level. Open up IIS Manager, click on root application, click on Request Filtering, if OPTIONS appears in list either remove or Allow Verb. Wish I had checked this first as lots of wasted time.
In my case, I missed the Microsoft.WebApi.Cors package.
Installed this package and configured it like so in the WebApiConfig class:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.EnableCors(new EnableCorsAttribute("*","*","*"));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Please fine-tune this before using in production because you probably don't want to have wild-cards for everything
This is what worked for me:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Check if URLScan tool is installed on IIS.
When so check following section:
;
; The verbs (aka HTTP methods) listed here are those commonly
; processed by a typical IIS server.
;
; Note that these entries are effective if "UseAllowVerbs=1"
; is set in the [Options] section above.
;
GET
HEAD
POST
OPTIONS
I know this is an old post, but I just went through the exact same problem.
In my situation, I have CORS installed for both OWIN and WebAPI. The OWIN CORS middleware was intercepting the OPTIONS call long before it ever made it to the WebAPI stuff. Maybe this well help someone else in the future.
I have installed Microsoft.AspNet.WebApi.Cors & Microsoft.Owin.Cors for my oWin based WebAPI and added app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); at config like below:
public class Startup : IStartup, IAppStartup
{
public void Configuration(IAppBuilder app)
{
var config = this.GetInjectionConfiguration();
BootstrapperWebApi bootstrapperWebApi = (BootstrapperWebApi)this.GetBootstrapperWebApi(config);
bootstrapperWebApi.Initialize(true)
.EnableLogging()
.DisableWebApiDefaultExceptionHandler();
WebApiConfig.Register(config);
app.UseOwinExceptionHandler();
app.Use<LoggerMiddleware>();
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
//others stuff
}
I tried all the mentioned posts but nothing worked for me, then i shifted my ASP.Net Web API 2 service to windows server 2012 (IIS 8.5) and same service worked without any changes. So issue was specific to IIS 7.5 on windows 7 machine.
In my case I did this:
<verbs allowUnlisted="true" applyToWebDAV="true">
<remove verb="OPTIONS"/>
<add verb="OPTIONS" allowed="true"/>
</verbs>
</requestFiltering>
</security>
When I added <add verb="OPTIONS" allowed="true"/> to the web.config, the application failed to start with this error
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Cannot add duplicate collection entry of type 'add' with unique key attribute 'verb' set to 'OPTIONS'
So I had to remove it first.
I have the same issue. The OPTIONS request return 200 OK status but it does not contain Access-Control-Allow-Origin header. The problem was our customer network policy blocking the OPTIONS verb request and response the warning message with 200 OK status. I know this is the old post but I want to share my case for anyone needed.
One more case, maybe it will save time for somebody. When I used config with HttpConfiguration.EnableCors all was working fine but when I used web.config file it was failing with CORS errors. It started work after I removed the .vs folder.
<figure>
<img src="https://i.stack.imgur.com/CbRyM.png" alt="">
<figcaption> change the OptionsVerbHangle</figcaption>
</figure>
<figure>
<img src="https://i.stack.imgur.com/wjcMV.png" alt="Minha Figura">
<figcaption>Adicione * and in the case of php use fastcgimodule</figcaption>
</figure>
<figure>
<img src="https://i.stack.imgur.com/wRwpi.png" alt="Minha Figura">
<figcaption>Mapping to folder
</figcaption>
</figure>
<figure>
<img src="https://i.stack.imgur.com/hhqJi.png" alt="Minha Figura">
<figcaption>all verbs
</figcaption>
</figure>
<figure>
<img src="https://i.stack.imgur.com/86kKX.png" alt="Minha Figura">
<figcaption>Select script
</figcaption>
</figure>
Just follow the images below to unlock the colors in IIS
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here

Bootstrap 3 Glyphicons are not working in window mobile

Bootstrap 3 Glyphicons are not working in window mobile. They are working in other devices(Expect window phone)
As Mathew Hintzen has posted, the easiest way is to include the MIME specification in your web.config. Also, as he pointed out, this will cause 500 Internal Server Errors if you are using the Bundling of styles/scripts:
<configuration>
...
<system.webServer>
...
<staticContent>
...
<mimeMap fileExtension=".woff" mimeType="font/x-woff" />
</staticContent>
</system.webServer>
</configuration>
To answer the OP's question AND to fix the 500 Internal Server errors, include a 'remove' statement before setting the static content:
<configuration>
...
<system.webServer>
...
<staticContent>
...
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="font/x-woff" />
</staticContent>
</system.webServer>
</configuration>
This will load the font files on Windows Mobile / Internet Explorer Mobile (IE Mobile) AND the web server will continue to serve other bundled files to other browsers/clients.
I hope this helps. Happy Coding!
this is may because your Glyphicons font file is not responding from the server, add a MIME type like with [file name extension : .woff, MIME type : font/x-woff].
To add a MIME type ...
--->start
--->Internet Information Services manager (IIS manager)
---> select the site
--->add a MIME type like with [file name extension : .woff, MIME type : font/x-woff].
The easiest way is to include the MIME specification in your web.config as suggested by user2959178 in the comments above
<configuration>
...
<system.webServer>
...
<staticContent>
...
<mimeMap fileExtension=".woff" mimeType="font/x-woff" />
</staticContent>
</system.webServer>
</configuration>
WARNING: While this will fix the windows mobile, it CAN cause all other browsers to break with internal server errors of 500 if you are using the Bundling of Styles.
I have this issue as well.
My current solution is to use text fallbacks in buttons.
<button class="btn btn-default"><span class="glyphicon glyphicon-ok">
OK
</span></button>
It is not a design fix but it makes the product functional.
Note that we have tested several browsers on several different mobiles brands (ios, android, windows phone, firefox, chrome, IE 8, IE 11).
The problem only occurs on windows phone.
I have a Nokia Lumia 920 WIndows 8.
You can read a bit more on the bootstrap page for other documented fixes here: http://getbootstrap.com/getting-started/#browsers .

Orchard CMS 1.7 Custom Theme images not loading

I had started creating a new theme while using v1.6.1.
Things were not going well with a custom module creation so I decided to start from scratch. This time I am using the source from V1.7
I copied my custom theme folder, pasted it into the themes folder of the new 1.7 project and ran the site. I could see the theme in my dashboard so I set it to the current theme.
Now when I view my site NONE of the images are loading. The style sheets are loading, though none of the images - either from the style sheet or from any views - are loading.
My images are in myTheme/content/images - which as I understand it is how 1.6.1 required things to be laid out.
My content folder has a web.config as does my images folder. It's the same config used in 1.6.1 so I'm wondering if something has changed.
In one of my theme views I have the following code - this worked in 1.6.1:
<img src="#Url.Content(Html.ThemePath(WorkContext.CurrentTheme,"/Content/Images/phoneBullet.png"))" alt="T:" />
If I output this to my front-end I get:
~/Themes/PerformanceAbrasives/Content/Images/phoneBullet.png
This tells me things appear to be in the correct place - though I'm wondering if my web.config is now out of date?
I have this - there is a copy in content and a copy in images:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<!-- iis6 - for any request in this location, return via managed static file handler -->
<add path="*" verb="*" type="System.Web.StaticFileHandler" />
</httpHandlers>
</system.web>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
<handlers accessPolicy="Script,Read">
<!--
iis7 - for any request to a file exists on disk, return it via native http module.
accessPolicy 'Script' is to allow for a managed 404 page.
-->
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
Any pointers?
UPDATE
Ok... so if I set my project to use Visual Studios internal development server then all is well - if I tell it to use Local IIS Web server - use IIS Express (http://localhost:30333/) - then it doesn't work...
UPDATE 2
Ok - now running from IIS 7 and images do not load... stylesheets seem ok, but images are not.. grrr
any ideas so I don't run into this problem when I deploy?
Ok - I figured it....
You only need the web.config in my Content directory - not in both content AND images

How to use font-face to read font files in Umbraco?

I had my templates in simple HTML but when I put my templates, scripts and css in the Umbraco site. The fonts stopped being picked up.
At first I thought it was a path thing, but it wasn't. Whenever I try to type the URL of the referenced font I get an error.
Page not found No umbraco document matches the url
'localhost/.../bliss-heavy.otf'
umbraco tried this to match it using this xpath query'/root/*
[#urlName = "login"] | /root// [#urlName = "login"]')
I know my path is correct because when I try a slightly different path I get the obvious error:
Server Error in Application "LOCAL.DOTAGENCYLONDON.CO.UK"
Internet Information Services 7.5 Error Summary HTTP Error 404.0 - Not
Found The resource you are looking for has been removed, had its name
changed, or is temporarily unavailable.
In my web.config file I have:
<staticContent>
<remove fileExtension=".air" />
<remove fileExtension=".ttf" />
<remove fileExtension=".eot" />
<remove fileExtension=".svg" />
<remove fileExtension=".otf" />
<remove fileExtension=".woff" />
<mimeMap fileExtension=".air" mimeType="application/vnd.adobe.air-application-installer-package+zip" />
<mimeMap fileExtension=".ttf" mimeType="font/opentype" />
<mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
<mimeMap fileExtension=".otf" mimeType="font/otf" />
<mimeMap fileExtension=".woff" mimeType="application/x-woff" />
</staticContent>
What should I look for now?
You first error looks like when you try to preview or browse to an umbraco page that has no template selected - go to the properties tab of the page and see if there is a template chosen:
#amelvin, thanks for your answer. That happens because I have no template or even node for the the /login.aspx URL.
But why was site being redirect to /login.aspx? It turned out that the app pool user didn't have permission to read the font files.
Once I overwrote the app pool credentials with mine in the IIS website, it was solved.

Resources