ASP.NET Core 2.2 Web API Angular. 主机提供商说500 - 内部服务器错误

问题描述 投票:0回答:2

我正试图通过支持ASP.NET Core的托管提供商SmarterASP.NET发布ASP.NET Core 2.2 Web API和Angular 8项目。然而,我得到以下错误。

500 - 内部服务器错误。 你要找的资源有问题,无法显示。

然而,如果我在本地启动该项目,它可以完美地工作。

我在互联网和各种论坛上搜索了一下,看到的是 这个问题, 另一个问题这个github帖子.

这是我的 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()StartUp 类。

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但是,日志文件没有被创建。

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

c# angular asp.net-core asp.net-core-webapi asp.net-core-2.2
2个回答
3
投票

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

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

试一次!


1
投票

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


1
投票

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

所以我的错误是

  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() 工作不正确。

所以我把代码改成了。

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. 我加入了文件夹 wwwroot 在我的ASP.NET Core 2.2应用程序中放入Angular prod bundle。

如果有人需要指导如何从Visual Studio中进行部署,请联系我们。

© www.soinside.com 2019 - 2024. All rights reserved.