简体   繁体   English

ASP.NET Core:调用Web API返回500

[英]Asp.net core : Calling web api returns 500

I am new to asp.net core application. 我是asp.net核心应用程序的新手。

I have an asp.net core application which uses repository pattern. 我有一个使用存储库模式的asp.net核心应用程序。 It have an api controller named ApplicationsAPIController . 它有一个名为ApplicationsAPIController的api控制器。

ApplicationsAPIController has a GetApplications method which return all applications. ApplicationsAPIController有一个GetApplications方法,该方法返回所有应用程序。

I have app published to remote server. 我已将应用发布到远程服务器。 While calling GetApplications api from postman throws 500 Internal Server Error with following HTML as response: 从邮递员调用GetApplications api时,以以下HTML作为响应会引发500 Internal Server Error

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request. </h2>
<h3>Development Mode</h3>
<p>
    Swapping to 
    <strong>Development</strong> environment will display more detailed  information about the error that occurred.
</p>
<p>
    <strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the
    <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to
    <strong>Development</strong>, and restarting the application.
</p>

Note: Above api is working perfectly in development enviroment (locally). 注意:上面的api在开发环境(本地)中运行良好。

As per the error, i think i have to set up a staging or production environment. 根据错误,我认为我必须设置一个暂存或生产环境。 But i could not find any good and step by step document to setup. 但是我找不到任何好的逐步设置文件。 I have tried it many ways but failed. 我已经尝试了很多方法,但是都失败了。

The 500 error means your code encountered an issue, not that you forgot to assign correct environment. 500错误表示您的代码遇到问题,而不是您忘记分配正确的环境。 What you need to do is see what the actual error was. 您需要做的是查看实际错误是什么。 You have many options of which perhaps the fastest would be to enable dev-environment errors (consider temporarily adding app.UseDeveloperExceptionPage(); or setting up the environment as "DEV"). 您有许多选择,其中最快的选择是启用开发环境错误(考虑临时添加app.UseDeveloperExceptionPage();或将环境设置为“ DEV”)。 A more robust approach would be to add some application error handling and logging (review Exception Filters topic). 一种更可靠的方法是添加一些应用程序错误处理和日志记录(请参阅“异常过滤器”主题)。

I think you need enable the CORS in the WebAPI. 我认为您需要在WebAPI中启用CORS。

Here has a baby steps to enable. 这里有一个婴儿步骤启用。 After enable I had error 500 to. 启用后,我遇到了500错误。 https://developer.okta.com/blog/2018/04/26/build-crud-app-aspnetcore-angular#run-and-test-with-fiddler-or-postman https://developer.okta.com/blog/2018/04/26/build-crud-app-aspnetcore-angular#run-and-test-with-fiddler-or-postman

The sample bllow enables, but is unsafe. 样品吹气可以但不安全。

In Startup.cs, ConfigureService make 在Startup.cs中,ConfigureService使

services.AddCors(options =>
{
    options.AddPolicy("CorsPolicy",
        builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
});

And your method Configure use: 和你的方法配置使用:

app.UseCors("CorsPolicy");
app.UseMvc();

Will allow everyone access your API I recommend read Microsoft Docs about CORS. 将允许所有人访问您的API,我建议阅读有关CORS的Microsoft文档。 https://docs.microsoft.com/pt-br/aspnet/core/security/cors?view=aspnetcore-2.2 https://docs.microsoft.com/pt-br/aspnet/core/security/cors?view=aspnetcore-2.2

To resolve error 500 I see this topic. 要解决错误500,请参阅本主题。 I used configuration of the question and resolved my problem. 我使用问题的配置并解决了我的问题。 Getting CORS error *only* on server exception calling ASP.NET Core Web API from Angular 6 在从Angular 6调用ASP.NET Core Web API的服务器异常中仅获得CORS错误

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(o => o.AddPolicy("AllowAllPolicy", builder =>
    {
        builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
    }));

    services.AddMvc();
    services.AddOptions();
    services.Configure<AppConfig>(Configuration.GetSection("AppConfig"));
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseCors("AllowAllPolicy");

    app.UseMvc();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM