简体   繁体   中英

Asp.net core : Calling web api returns 500

I am new to asp.net core application.

I have an asp.net core application which uses repository pattern. It have an api controller named ApplicationsAPIController .

ApplicationsAPIController has a GetApplications method which return all applications.

I have app published to remote server. While calling GetApplications api from postman throws 500 Internal Server Error with following HTML as response:

<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).

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. 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"). 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.

Here has a baby steps to enable. After enable I had error 500 to. 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

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. https://docs.microsoft.com/pt-br/aspnet/core/security/cors?view=aspnetcore-2.2

To resolve error 500 I see this topic. 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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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