-->

ASP.NET Core app not working after publish to Azur

2020-03-03 09:38发布

问题:

I have an ASP.NET Core app which is running fine locally.

However, when I publish (Web Deploy) the site to Azure, I get a 403: You do not have permission to view this directory or page.

I have a default controller and a route defined in Startup.cs:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });
            });

The folder structure after web deploy to Azure looks like:

|_ 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?)

Any idea of what I should look for in project.json or the Kudu portal to figure out what's wrong?

My project.json file:

{
  "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/"
  }
}

Edit:

For one, I was missing the Microsoft.AspNetCore.Server.IISIntegration NuGet package. When I added it, I also got a web.config file in the site root (which I included through project.json).

I also added .UseIISIntegration() to my WebHostBuilder in Startup.cs:

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

I can now run it on IIS Express locally (although I guess it's IIS fronting Kestrel?), but when published to Azure I get error: HTTP Error 502.5 - Process Failure

The event log in Azure states: Failed to start process with commandline '"%LAUNCHER_PATH%" %LAUNCHER_ARGS%', ErrorCode = '0x80070002'.

According to the documentation, the troubleshooting step is:

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.

Not sure how this applies to Azure, though?

回答1:

The problem came from insufficient IIS integration/configuration.

I had to add the following to project.json:

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

I also added a postPublish script for IIS publishing:

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

I also added the Microsoft.AspNetCore.Server.IISIntegration NuGet package:

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

This created a web.config file, which I had to modify as:

<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>

Finally I added UseIISIntegration to my WebHostBuilder:

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

A standard Publish (Web Deploy) from Visual Studio and the website started just fine on Azure (although in my case I also had to add a prepublish script for some Gulp tasks).