ASP.net core 1.0 web.config 被覆盖导致 CGI 异常。

6
我有一个可工作的web.config,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\Example.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

但不知何故,Visual Studio 正在更新我的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

这段代码可以通过Visual Studio的发布菜单实现(并且在部署到Azure Web应用程序后也可以正常工作)。但是,如果使用dotnet CLI(如dotnet publish)则无法正常工作,因为它会保留web.config中的变量:%LAUNCHER_PATH%和%LAUNCHER_ARGS%,而不是我想要的:dotnet和.\Example.dll。
注意:当使用命令行执行dotnet restore和dotnet build时,我的构建服务器不会污染web.config。当使用MSBuild构建我的sln时也不会。我在本地和构建服务器上都安装了Visual Studio 2015,并验证了我的“dotnet”CLI版本匹配。
如何通过回滚每次提交之前的web.config来避免与Visual Studio冲突?很明显我做错了什么,这应该是一个简单的配置修复吗?
更新:
Startup.cs
public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

Appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

Program.cs

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

Project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true,

    "exclude": [
      "wwwroot",
      "typings",
      "node_modules"
    ],
    "publishExclude": [
      "**.user",
      "**.vspscc"
    ]
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": "portable-net45+win8+netstandard1.6"
    },

    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": "portable-net45+win8+netstandard1.6"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "netstandard1.4",
        "dnxcore50"
      ],
      "dependencies": {
        "Microsoft.AspNetCore.Diagnostics": "1.0.0",
        "Microsoft.AspNetCore.Mvc": "1.0.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0",
        "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": "1.0.0",
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Logging.Debug": "1.0.0",
        "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
        "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
        "Microsoft.AspNetCore.Hosting": "1.0.0",
        "System.ServiceModel.Primitives": "4.1.0",
        "System.ServiceModel.Http": "4.1.0",
        "System.Private.ServiceModel": "4.1.0",
        "Presentation.Common": "*",
        "System.Runtime": "4.1.0",
        "System.Runtime.Numerics": "4.0.1",
        "SharedContract": "*"
      }
    }
  },

  "runtimes": {
    "win10-x64": {},
    "win10-x86": {},
    "win8-x64": {},
    "win8-x86": {}
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "gulp rebuild", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },

  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-clean": "^0.3.2",
    "gulp-concat": "^2.6.0",
    "gulp-less": "3.0.2",
    "gulp-tsc": "^1.1.5",
    "gulp-typescript": "^2.13.1",
    "lite-server": "^2.2.0",
    "path": "^0.12.7",
    "rimraf": "2.3.2",
    "typescript": "^1.8.10",
    "typings": "^0.8.1"
  }
}

1
我后来了解到,这是在我点击Visual Studio中的运行按钮以本地查看网站时发生的。但我仍然不确定如何解决这个问题。 - Dessus
2个回答

0

发布到IIS

发布到IIS工具可以添加到任何.NET Core应用程序中,并通过创建或修改web.config文件来配置ASP.NET Core模块。该工具在使用dotnet publish命令或使用Visual Studio进行发布后运行,并将为您配置processPath和arguments。

从project.json的发布后脚本中移除dotnet publish-iis将停止自动更新。


我在我的project.json中没有后发布脚本,但仍然发生这种情况。 - Dave Thieben

0
最好将配置设置迁移到 JSON 文件中,例如 appsettings.json,然后在 startup.cs 中使用 ConfigurationBuilder 将这些文件设置为配置源。

我不知道如何做这个。你能详细说明一下吗?有可能我有一些RC2的设置文件,但我遵循了许多指南,与干净的1.0项目进行了比较。 - Dessus
我知道我的目标是什么,但不确定如何实现。我想我可以在某个地方的 JSON 文件中设置这些变量的值。 - Dessus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接