Created
May 22, 2016 04:54
-
-
Save EmmanuelTsouris/ccfc11139cc781d20b60457107f5cf2c to your computer and use it in GitHub Desktop.
Hosting an Angular 2 App on Windows 2008 (IIS 7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Allowing the Plus (+) character in a URL for Angular2 html5 Routing | |
By default, IIS 7 denies requests with certain characters in the URL. The plus (+) sign is one of those illegal characters. You can disable the setting for your web app using the web.config, basically setting allowDoubleEscaping to true. | |
Before making this change in production, be sure to research and understand the security implications for your specific environment, server, and web application. | |
Example location in web.config | |
``` | |
<configuration> | |
<system.webServer> | |
<security> | |
<requestFiltering allowDoubleEscaping="true" /> | |
</security> | |
</system.webServer> | |
</configuration> | |
``` | |
[Learn more about requestFiltering](https://www.iis.net/configreference/system.webserver/security/requestfiltering) | |
Attribute | |
Description | |
allowDoubleEscaping | |
Optional Boolean attribute. | |
If set to true, request filtering will allow URLs with doubly-escaped characters. If set to false, request filtering will deny the request if characters that have been escaped twice are present in URLs. | |
The default value is false. | |
#Json file | |
Windows 2008 IIS 7 doesn't usually let you download a json file with the default configuration. You'll receive a 404 error, and need to add a mime type to handle the file extension. Adding a mime type can be done through the IIS console, or simply by editing the web.config. | |
Example location in web.config | |
``` | |
<configuration> | |
<system.webServer> | |
<staticContent> | |
<mimeMap fileExtension=".json" mimeType="application/json" /> | |
</staticContent> | |
</system.webServer> | |
</configuration> | |
``` | |
#URL rewrite and Routing | |
Another challenge with running Angular2 on Windows 2008 with IIS is the component router and the default html5 routes. | |
For URL rewriting, you'll need the rewrite module. | |
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module | |
An example rule for this Angular2 application | |
``` | |
<rewrite> | |
<rules> | |
<rule name="Main Rule" stopProcessing="true"> | |
<match url=".*" /> | |
<conditions logicalGrouping="MatchAll"> | |
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> | |
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> | |
</conditions> | |
<action type="Rewrite" url="index.html" /> | |
</rule> | |
</rules> | |
</rewrite> | |
``` | |
#Example of a working web.config (includes server side caching config) | |
``` | |
<?xml version="1.0" encoding="utf-8"?> | |
<!-- | |
For more information on how to configure your ASP.NET application, please visit | |
http://go.microsoft.com/fwlink/?LinkId=169433 | |
--> | |
<configuration> | |
<system.web> | |
<compilation debug="true" targetFramework="4.5" /> | |
<httpRuntime targetFramework="4.5" /> | |
</system.web> | |
<system.webServer> | |
<modules runAllManagedModulesForAllRequests="true" /> | |
<security> | |
<requestFiltering allowDoubleEscaping="true" /> | |
</security> | |
<staticContent> | |
<remove fileExtension=".map" /> | |
<mimeMap fileExtension=".json" mimeType="application/json" /> | |
<mimeMap fileExtension=".map" mimeType="application/json" /> | |
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> | |
</staticContent> | |
<rewrite> | |
<rules> | |
<rule name="Main Rule" stopProcessing="true"> | |
<match url=".*" /> | |
<conditions logicalGrouping="MatchAll"> | |
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> | |
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> | |
</conditions> | |
<action type="Rewrite" url="index.html" /> | |
</rule> | |
</rules> | |
</rewrite> | |
<caching> | |
<profiles> | |
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" /> | |
<add extension=".html" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" /> | |
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" /> | |
</profiles> | |
</caching> | |
</system.webServer> | |
</configuration> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment