ASP.NET 核心 2.2 Web API Angular。托管服务提供商说 500 - 内部服务器错误

ASP.NET Core 2.2 Web API Angular. Hosting provider says 500 - Internal server error

我正在尝试使用支持 ASP.NET Core 的托管服务提供商 SmarterASP.NET 发布 ASP.NET Core 2.2 Web API 和 Angular 8 项目。但是,我收到以下错误:

500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

但是,如果我在本地启动该项目,该项目将完美运行。

我通过互联网和各种论坛进行了搜索,看到 , and this github post

这是我的Main方法:

public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateWebHostBuilder(args).Build();
        WebHost.CreateDefaultBuilder(args)
            .UseSetting(WebHostDefaults.DetailedErrorsKey, "true");

        host.Run();
    }
}

这是我的 Configure() of StartUp class:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    IServiceProvider provider)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }       
    app.Use(async (context, next) =>
    {
        await next();
        if (context.Response.StatusCode == 404 && 
            !Path.HasExtension(context.Request.Path.Value))
        {
            context.Request.Path = "/index.html";
            await next();
        }
    });     
    app.ConfigureCustomExceptionMiddleware();
    app.UseCors("ApiCorsPolicy");
    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles")),
            RequestPath = new PathString("/StaticFiles")
    });
    app.UseHttpsRedirection();
    app.UseAuthentication();
    app.UseMvc();
    app.UseDeveloperExceptionPage();
}

这是我的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <httpErrors errorMode="Detailed" />
      <aspNetCore processPath="dotnet">
      <environmentVariables>
           <environmentVariable name="ASPNETCORE_DETAILEDERRORS" value="true" />
           </environmentVariables>
      </aspNetCore>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" 
            resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\fooappl.dll" stdoutLogEnabled="true" 
          stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
    </system.webServer>
  </location>
  <system.web>
    <customErrors mode="Off"/>
  </system.web>
</configuration>
<!--ProjectGuid: 25e0e882-c3ff-4f8d-98cc-f26a692fa019-->

我创建了文件夹 logs\stdout,但是没有创建日志文件。

我有点卡住了。你能说我做错了什么吗?

我遇到了类似的问题,我通过在配置文件中提供 dotnet exe 的完整路径解决了这个问题。根据您的服务器路径更新进程路径:

<aspNetCore processPath="C:\Program Files\dotnet\dotnet.exe" arguments=".\fooappl.dll" stdoutLogEnabled="true" 
      stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />

试一次!

尝试从您的 web.config 文件中删除 hostingModel="InProcess"。

我已经成功部署了我的应用程序!也许它会对其他人有所帮助。

所以我的错误是:

  1. 不正确web.config。感谢 SmarterAsp.Net 的朋友们!这是显示详细错误的正确 web.config

    <?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="dotnet" arguments=".\yourAppl.dll" 
                stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" 
                hostingModel="InProcess" >
                <environmentVariables>
                    <environmentVariable name="ASPNETCORE_ENVIRONMENT" 
                        value="Development" />
                </environmentVariables>     
             </aspNetCore>
          </system.webServer>
       </location>
    </configuration>
    <!--ProjectGuid: 25e0e882-c3ff-4f8d-98cc-f26a692fa019-->
    
  2. 修复 web.config 后,会显示详细的错误。错误是 FileNotFoundException。原因是 Directory.GetCurrentDirectory() works incorrectly.

所以我将代码更改为:

private IHostingEnvironment _env;

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    Configuration = configuration;
    _env = env;
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
{   
    // The other code is omitted for the brevity
    app.UseStaticFiles(new StaticFileOptions()
    {                
        FileProvider = new PhysicalFileProvider(Path.Combine(_env.ContentRootPath, @"StaticFiles")),
        RequestPath = new PathString(Path.Combine(_env.ContentRootPath, "/StaticFiles"))
    });
}
  1. 我在 ASP.NET Core 2.2 应用程序中添加了文件夹 wwwroot,并将 Angular prod bundle 放入其中

If somebody would need a guide how to deploy from Visual Studio