ASP.NET 发布到 Azure 后核心应用无法运行

ASP.NET Core app not working after publish to Azure

我有一个 ASP.NET 核心应用程序,它在本地 运行 运行良好。

但是,当我发布(Web 部署)站点到 Azure 时,我得到 403You do not have permission to view this directory or page

我在 Startup.cs:

中定义了默认控制器和路由
app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });
            });

Web 部署到 Azure 后的文件夹结构如下所示:

|_ home
  |_ site
    |_ wwwroot (contains DLL's and files specified through 'publishOptions' in project.json)
      |_ wwwroot (the contents of the 'wwwroot' folder I have in Visual Studio)
      |_ Views (contains MVC views)
      |_ refs (contains referenced DLLs, I think?)

知道我应该在 project.json 或 Kudu 门户中查找什么以找出问题所在吗?

我的 project.json 文件:

{
  "title": "My Web App",
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "compile": {
      "exclude": [ "bin/**", "obj/**", "node_modules/" ]
    }
  },
  "publishOptions": {
    "include": [ "wwwroot", "Views", "appsettings.json", "appsettings.*.json" ]
  },
  "scripts": {
    "prepublish": [ "jspm install", "gulp build" ]
  },
  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "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",
    "System.IO.FileSystem": "4.0.1"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  },
  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://*:8000/"
  }
}

编辑:

首先,我缺少 Microsoft.AspNetCore.Server.IISIntegration NuGet 包。当我添加它时,我还在站点根目录中获得了一个 web.config 文件(我通过 project.json 包含了它)。

我还在Startup.cs中的WebHostBuilder中添加了.UseIISIntegration():

public static void Main(string[] args)
{
   new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseStartup<Startup>()
      .UseIISIntegration() // This was missing
      .Build()
      .Run();
   }

我现在可以在 IIS Express 本地 运行 它(虽然我猜它是 IIS fronting Kestrel?),但是当发布到 Azure 时我得到错误: HTTP Error 502.5 - Process Failure

Azure 中的事件日志状态:Failed to start process with commandline '"%LAUNCHER_PATH%" %LAUNCHER_ARGS%', ErrorCode = '0x80070002'.

根据文档,故障排除步骤是:

If the server does not have Internet access while installing the server hosting bundle, this exception will ensue when the installer is prevented from obtaining the Microsoft Visual C++ 2015 Redistributable (x64) packages online. You may obtain an installer for the packages from the Microsoft Download Center.

但不确定这如何适用于 Azure

问题来自 IIS 不足 integration/configuration。

我必须将以下内容添加到 project.json

"tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  }

我还为 IIS 发布添加了 postPublish 脚本:

"scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }

我还添加了 Microsoft.AspNetCore.Server.IISIntegration NuGet 包:

"dependencies": {
  "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0"
}

这创建了一个 web.config 文件,我必须将其修改为:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\My.Web.App.dll" arguments="" forwardWindowsAuthToken="false" stdoutLogEnabled="true" stdoutLogFile="\?\%home%\LogFiles\stdout" />
  </system.webServer>
</configuration>

最后我将 UseIISIntegration 添加到我的 WebHostBuilder:

public static void Main(string[] args)
{
   new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseStartup<Startup>()
      .UseIISIntegration() // This was missing
      .Build()
      .Run();
   }

来自 Visual Studio 的标准 Publish(Web 部署)并且该网站在 Azure 上启动得很好(尽管在我的情况下我还必须添加一个 prepublish 一些 Gulp 任务的脚本。