Don't Throw Away All Those web.config Settings

Tuesday, September 6, 2016

ASP.NET Core might not use a complicated hierarchy of XML configuration files anymore, but if you host under IIS, then IIS and web.config are still the best of friends. There is some XML configuration required to run Core under IIS, specifically the part to reverse proxy all incoming requests over to the ASP.NET Core module and ultimately into the Kestrel server.

Other pieces of web.config still work in the new world of ASP.NET, too. For example, IIS will still honor rewrite rules in web.config.

Here is a sample web.config to enforce lower case URLs that also proxies to the ASP.NET Core Module.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>

    <rewrite>
      <rules>
        <rule name="Convert to lower case" stopProcessing="true">
          <match url=".*[A-Z].*" ignoreCase="false" />
          <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    
    <handlers>
      <add name="aspNetCore" path="*" verb="*" 
           modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" 
                stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" 
                forwardWindowsAuthToken="false"/>
    
  </system.webServer>
</configuration>


Comments
gravatar Khalid Abuhakmeh Tuesday, September 6, 2016
The latest ASP.NET Community Standup showed that the interns at Microsoft have functioning rewrite middleware that can read the rewrite rules. This means the config will still be valuable, but will run at the ASP.NET Core Module level, rather than at the IIS level.
gravatar Andrew Lock Tuesday, September 6, 2016
Just to add to Khalid's comment, you can find the rewrite middleware they have written here: https://github.com/aspnet/BasicMiddleware
Hrvoje Tuesday, September 6, 2016
@Khalid still, i would like to keep some rewrite rules on proxy level, for reverse proxy functionality. IMHO, app is not where rewrite rules should live, except for some specific reason. Also, interns middleware vs 10y of battle-tested url rewrite module, hard to compare even.
gravatar Matthew Tuesday, September 6, 2016
Everything you need can be done with middlewares, so those are the way to go. Of course you can further use IIS features, but the whole ASP.NET Core stuff is about getting rid of tight coupling to IIS and the Windows platform. And: when you want to debug locally from command line in Kestrel (dotnet run), you can get unexpected results when you rely on IIS functionality. So I would vote for leaving web.config as empty as possible.
Hrvoje Tuesday, September 6, 2016
Rewrite rules, not related to some specific app logic, should be part of reverse proxy (nginx or iis), not app/middleware. Kestrel shouldn't be exposed to the public, and reverse proxy job is to handle those general URL rules (enforce https, add trailing /, lowercase, ...), and also throttling, security, etc. They do that job much better than kestrel.
Comments are closed.

My Pluralsight Courses

K.Scott Allen OdeToCode by K. Scott Allen
What JavaScript Developers Should Know About ECMAScript 2015
The Podcast!