Azure and .json mimeType without web.config - azure

Adding to my web.config
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
Allows my application to run on Azure, but will crash my remote IIS server because its already included. Removing the remote IIS mimeType is not practical in this particular case. I end up using a different web.config
Is there another mechanism by which I can configure Azure IIS mimeType so I don't have this problematic web.config?
I would like a single deployment package that will work on Azure and non Azure.

This should work:
<system.webServer>
<staticContent>
<remove fileExtension=".json" />
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
See also here: http://blogs.msdn.com/b/chaun/archive/2009/12/04/iis7-error-cannot-add-duplicate-collection-entry-of-type-mimemap-with-unique-key-attribute-fileextension.aspx
This doesn't make any difference to your overall IIS configuration, it just conditionally removes the mimeMap from the configuration of this particular site (as governed by this web.config) before adding it again.

You can create a startup task that adds the mime type on IIS level. This way you won't need to include it in your web.config:
"%windir%\System32\inetsrv\appcmd.exe" set config /section:staticContent /+"[fileExtension='.json',mimeType='application/json']"
exit /b 0

Related

CORS Error with ttf font file on Azure CDN

I have a CDN I have created using the Verizon Premium SKU. when it comes to fonts I get "from origin 'https://myfqdn.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource"
However, I have followed this doc https://learn.microsoft.com/en-us/azure/cdn/cdn-cors with no luck.
However, if I go to my https://cdn.myfqdn.com (yes I have a custom domain and https enabled) the page loads however with no issues.
Here is the XML from the rule that I created from the doc above.
<rules schema-version="2" rulesetversion="6" rulesetid="945266" xmlns="http://www.whitecdn.com/schemas/rules/2.0/rulesSchema.xsd">
<rule id="1823263" platform="http-large" status="active" version="3" custid="A76A4">
<!--Changed by userId: 952 on 02/25/2019 03:45:01 PM GMT-->
<!--Changed by xxx#cdn.windowsazure.com on 02/25/2019 03:25:23 PM GMT from IP: xxx.xxx.xxx.xxx-->
<description>Wildcard</description>
<!--If-->
<match.request-header.wildcard name="Origin" result="match" value="Https://myFQDN.com" ignore-case="true">
<feature.set-request-header action="set" key="Access-Control-Allow-Origin" value="*" />
<feature.set-request-header action="set" key="Access-Control-Allow-Headers" value="*" />
<feature.set-request-header action="set" key="Access-Control-Allow-Methods" value="GET, HEAD, OPTIONS" />
<feature.set-request-header action="set" key="Access-Control-Expose-Headers" value="*" />
</match.request-header.wildcard>
</rule>
</rules>
Thanks for your help
I was also facing this issue in my blog. All the files were loading from the CDN except fonts and icons. I had to do two things. One is to allow all the origins in the Azure web app and the other is to configure the files in IIS by editing the web.config.
Just login to your Azure portal, and go to your Azure web application. Click on the CORS menu under API and * as the allowed origin.
By default files with .woff2, .woff, and .ttf extensions are not served by IIS in Azure App Service. That is the reason for this CORS issue. We will add these configurations in the system.webServer tag which is the child tag of configuration tag.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension="woff" mimeType="application/font-woff" />
<mimeMap fileExtension="woff2" mimeType="application/font-woff2" />
<mimeMap fileExtension=".ttf" mimeType="application/octet-stream" />
<mimeMap fileExtension=".ttc" mimeType="application/octet-stream" />
<mimeMap fileExtension=".otf" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
You can also do this by adding the extension called “Enable Static Web Fonts”. I have written a detailed blog about this, you can read it here.

Configure well-known folder in MVC 5 Web App?

I'm having problems creating and accessing a well-known folder in an MVC 5 Web App. I need the folder to house some documents for iOS and Android mobile apps.
First, I add a well-known folder to my MVC 5 Web App. I could not add a .well-known folder. Since I could not add the "." in front of the folder, I added a virtual directory using the Azure Portal.
This seems to work, but When I try to access the files using https://portal.mydomain.com/.well-known/apple-app-site-association or this https://portal.mydomain.com/.well-known/assetlinks.json, I get the following message.
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
When I try to access just the directory using https://portal.mydomain.com/.well-known/ I get the following message.
"You do not have permission to view this directory or page."
Finally, I added this to my web.config in order to handle the lack of a file extension on the apple-app-site-association file.
<staticContent>
<mimeMap fileExtension="." mimeType="application/octet-stream" />
</staticContent>
Any help is much appreciated. Thanks!
I added a new web.config file to my well-known folder and configured it like so. I can now access both files. This is a new web.config and has nothing to do with the web.config in the root directory. Using this config, the apple-app-site-association downloads a file when I access it using a web browser. The assetlinks.json file displays in the web page, so they behave differently.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension="." mimeType="application/octet-stream" />
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
</configuration>
I commented out this entry in my root directory web.config because I did not need it.
<staticContent>
<mimeMap fileExtension="." mimeType="application/octet-stream" />
</staticContent>

Enable Compression Mime-types for Web-Site Application

Our website uses both dynamic and static compression. I know that compression can be enabled/disabled on a web.config level, but that the mime-types for static and dynamic compression cannot be enabled at a web-config level.
Meaning, this section:
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" staticCompressionIgnoreHitFrequency="true">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<staticTypes>
Stuff
</staticTypes>
<dynamicTypes>
Stuff
</dynamicTypes>
</httpCompression>
Must go in the applicationHost.config, and is generally edited using appcmd.exe.
I know there is a location element in the applicationHost.config that allows setting many things on a per website basis, but I can't seem to find anywhere if mimetypes for dynamic compression are one of them.
I have tried overriding these settings using a location element, but have not had any success and cannot find documentation stating it's possible for the httpCompression element.
To make matters worse, we install our product as a web application under the default site, so really we want to enable these dynamic compression mime-types only under our application, instead of site (or server) wide. Is this possible?
Generally, we are using IIS 7 and above. Right now our minimum is 7, so assume anything needs to work with that.
My question is:
Can httpCompression settings be set in the applicationHost.config per website and possible per web application under a web site?
Is there a different way to enable dynamicCompression specifics on a website/web application level?
Just an important precision: There is one prerequisite to ensure that you can add MIME Types in the "web.config" file:
It is possible to add MIME Types in the <staticTypes> and <dynamicTypes> sections at the website level (in "web.config") only if this is explicitely allowed at the "applicationHost.config" level, as explained in this solution from Stack Overflow:
The important thing to note is that modifying your
applicationHost.config (in %windir%\system32\inetsrv\config) from the following setting:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
to:
<section name="httpCompression" overrideModeDefault="Allow" />
will enable configuration of the httpCompression tag under the
system.webServer tag in your web.config.
Yes you can very well add dynamic and static types in web application's web.config file. ApplicationHost.config will define global compression settings and if you want to override them in your application you can do so. Following is sample from one of my application.
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" />
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
</handlers>
<httpCompression>
<dynamicTypes>
<remove mimeType="text/*" />
<add mimeType="application/json" enabled="true" />
</dynamicTypes>
</httpCompression>
Here remove tag in dynamicTypes removes global entry coming from ApplicationHost.config
add tag is adding additional mimeType on top of global entries from applicationHost.config. This addition will be applicable only for whose web.config is being modified.
Similarly you can modify staticTypes as well.

Cannot get caching for static recources (CSS, images) does to work in Azure Web App

I have an Azure account with a Web Application that is in the very early stages of development.
I am using Google Pagespeed Insights to test for any performance issues ( https://developers.google.com/speed/pagespeed/insights/ )
It tells me I don't have the cache set for my static resources. So I have enabled caching in the web.config file for my web application by adding the following code:
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMaxAge="00.12:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
</system.webServer>
Yet this does not work (I check the headers with Chrome Dev Tools).
Any ideas on what I am doing wrong?
Thank you.
Never mind, got it figured out.
Had to take out the following code out of web.config:
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
It was there from the ASP.NET 5 MVC project I set up earlier.

Disable IIS Request Filtering for certain paths

Is there any way I can have IIS 7.0+ (or 7.5+) configured such that for certain paths Request Filtering is completely disabled. That is,
http://host.local/foo/bar.cs
is forbidden (since serving *.cs files is explicitly forbidden in applicationHost.config), but
http://host.local/foo/allow-all/bar.cs
is allowed.
In your allow-all directory, you can create a web.config file with the following configuration:
<configuration>
<system.webServer>
<security>
<requestFiltering>
<fileExtensions>
<remove fileExtension=".cs" />
</fileExtensions>
</requestFiltering>
</security>
<staticContent>
<mimeMap fileExtension=".cs" mimeType="text/plain" />
</staticContent>
</system.webServer>
</configuration>
This configuration removes the .cs extension from the request filtering. Additionally, for IIS to properly serve content, it needs a MIME type, so the .cs extension is added as text/plain.
These changes will also apply to all child directories of allow-all. This configuration works with an Integrated App Pool. Classic may require additional changes since there are HTTP handlers that explicitly disallow .cs as well.

Resources