C# .Net核心API返回500内部服务器错误

C# .Net核心API返回500内部服务器错误,c#,api,.net-core,http-status-code-500,C#,Api,.net Core,Http Status Code 500,我有一个.NET核心web api。当从邮递员处请求但返回时,它会按预期工作 Status Code: 500 Internal Server Error Referrer Policy: strict-origin-when-cross-origin 当从其他客户端请求时。我已经在Startup.cs中启用了CORS,如下所示: public void ConfigureServices(IServiceCollection services) { services.

我有一个.NET核心web api。当从邮递员处请求但返回时,它会按预期工作

Status Code: 500 Internal Server Error
Referrer Policy: strict-origin-when-cross-origin
当从其他客户端请求时。我已经在
Startup.cs
中启用了CORS,如下所示:

public void ConfigureServices(IServiceCollection services)
{
            services.AddCors();
            services.AddMvc();
            ...
}
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
            app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
            ...
}

但是仍然获取
状态代码:500内部服务器错误
。我试图解决这个问题已经太久了。请帮忙。谢谢。

在配置方法中添加以下内容:

// global cors policy
        app.UseCors(x => x
            .SetIsOriginAllowed(origin => true)
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
如果问题没有解决,那么添加一个自定义中间件并检查您的请求,您可以使用ilogger记录异常


遵循中间件的此链接:

这几乎肯定是由于出现了异常-您应该查看服务器日志,以了解有关出错原因的更多详细信息。这解决了我的问题。我的代码中有一个错误。我刚从服务器事件查看器中找到它。谢谢你@JonSkeet。你刚刚救了我的命。