Porting a Web.Config to ASPNET5

2019-05-17 18:49发布

问题:

I upgrading my project to ASPNET5. I'm hitting a snag regarding upgrading my web.config file.

I tried using Microsoft.Framework.ConfigurationModel.Xml package to read a URL Rewrite configuration. The config looks like:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="MainRule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
          </conditions>
          <action type="Rewrite" url="default.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

but of course I'm getting duplicate key issues when it tries to convert it to the object that aspnet5 uses.

Whats the best way to port your existing web.config to the new aspnet model? This is a small case I realize but in the real world these configs are really intense.

I've created an example project that I'm hoping to share with others when i get a few of these cases figured out.

回答1:

the web.config file you posted only has url rewrite rules for iis. Do you really need to access those from your app? IIS will read those directly from the web.config but you shouldn't need that stuff in your app so you don't need to try to parse that file with the new classes in Microsoft.Framework.Configuration at all.

if your app needs some other settings then you might as well use a json.config file as shown in many examples for your own application settings.

you can still drop that web.config file into the application root and assuming your app is hosted in IIS it should still do its job and tell IIS to do some url rewriting. you should not rename it to config.xml as it appears you have done, since IIS won't notice the file unless it is named web.config

why would you need to access those url rewrite rules from application code at all since it is IIS and not application code that does the url rewriting?

I think the new paradigm is use json.config and/or environment variables in azure for application configuration

The only thing you might still use a web.config file for is IIS configuration but that is separate from application configuration and that is the only thing you would use web.config files for going forward and you should not need to access web.config file from application code at all.