首页 > 解决方案 > Web API works locally but doesn’t work on azure

问题描述

I have created a web API connected to azure sql server in .net core using visual studio for Mac . Then I created a web app in azure and then published by project directly in visual studio for Mac to azure.

After I published I try to access the api using postman and chrome (URL/api/menu) but I got 500 server error which is generic and doesn’t tell me anything.

In visual studio for Mac I got the green light it said published and directly took me to the new url.

So, what do you guys thing is the problem.

This is my first time using azure so I didn’t change any setting or anything

标签: visual-studioasp.net-core-webapiazure-web-app-service

解决方案


由于许多不同的问题都可能导致此错误页面,因此我强烈推荐以下方法,以便快速轻松地确定根本原因,而无需使用 Azure(或任何服务器/平台)来获取日志。

您可以在启动时启用非常有用的错误消息,方法是设置 . UseSetting("detailedErrors", "true")和文件.CaptureStartupErrors(true)中的操作Program.cs

对于 ASP.NET 核心 2.1

public class Program  
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .CaptureStartupErrors(true)
            .UseSetting("detailedErrors", "true")
            .UseStartup<Startup>()
            .Build();
}

在你的 startup.cs 类中添加这些命令:

app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();

还在您的 web.config 文件中启用 stdoutLog

stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"

Web api 中的错误代码 500 通常表示 Startup.cs 中的配置问题 - 最常见的问题包括 DB 本身的问题、迁移问题(如果您使用 Code First 方法)、appsettings.js 的问题。

请参考 .\logs\stdout 中的日志文件。

希望能帮助到你。


推荐阅读