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

标签 asp.net .net build visual-studio-2015 asp.net-core

我有一个有效的 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(例如 dotnetpublish),它就不起作用,因为它会保留 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 发生冲突?我显然做错了什么,这应该是一个简单的配置修复?

更新:

启动.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();
}

应用程序设置.json

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

程序.cs

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

    host.Run();
}

项目.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"
  }
}

最佳答案

最好迁移 json 文件(如 appsettings.json)中的配置设置,然后使用startup.cs 中的configurationbuilder 将这些文件设置为配置源。

关于ASP.net core 1.0 web.config 被覆盖导致 CGI 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38408415/

相关文章:

javascript - 在 div 标签中加载 aspx 页面

c# - "When the site administrator has locked access to this section using <location allowOverride="假 "> from an inherited configuration file."

c# - 使用客户端脚本进行验证?

.net - .Net套接字-10054错误

c# - 在 OpenFileDialog 中使用 DialogResult.OK 时出错

mysql - 运行 spring boot jar 文件时出错(创建名称为 'dataSource' 的 bean 时出错)

git - TFS Git 项目中的依赖项目 VNEXT 构建

c# - Visual Studio 多项目解决方案选项

android - 1.12版后如何在Flutter中禁用Proguard?

c# - 在谈论各自的规则时,StyleCop 和 Code Analysis 之间有哪些区别?