简体   繁体   中英

Hardcoded paths in autogenerated web.config when using IIS with .NET Core 2.2 Web API

A system I work on is made of multiple .NET Framework APIs, to avoid having to run multiple instances of Visual Studio when working on the system we have set up the APIs to run locally in IIS rather than IIS Express.

I am now trying to do the same for a new .NET Core 2.2 Web API but I am having some trouble with the web.config file Visual Studio autogenerates whenever I launch the application.

This is a fresh .NET Core 2.2 Web API generated with VS 2019. After changing the project to run in IIS instead of IIS Express launchSettings.json looks like

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "http://localhost/ExampleApi",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:52245",
      "sslPort": 44376
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IIS",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ExampleApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

The web.config file Visual Studio generates looks like

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="C:\Program Files\dotnet\dotnet.exe" arguments="exec &quot;C:\Users\Richard\Dev\ExampleApi\ExampleApi\bin\Debug\netcoreapp2.2\ExampleApi.dll&quot;" stdoutLogEnabled="false" hostingModel="InProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

Note the hardcoded, machine specific paths in aspNetCore . If I delete the paths/elements as soon as I launch the API again the paths are added again.

This means that web.config would need to be gitignored otherwise each developer that runs the API is going to change this file. However, gitignoring the file means that we'd be unable to use a web.config file for IIS settings if we needed to.

Is there a way to avoid this? If not, is there anything major that you can do in web.config that cannot be done in .NET Core middleware or code configuration?

When I've looked up how to do things like set maximum request timeouts or rewrite rules the answers I find seem to be mixed between code and web.config eg example 1 , link 2

Project files :

<PropertyGroup>
  <ANCMPreConfiguredForIIS>true</ANCMPreConfiguredForIIS>
</PropertyGroup>

Web.config ;

<aspNetCore processPath="dotnet" arguments="exec &quot;.\bin\Debug\netcoreapp2.2\YOUR_PROJECT.dll&quot;" stdoutLogEnabled="true" stdoutLogFile="..\logs\" hostingModel="inprocess">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM