Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core API - Get 404 on Azure App Service, but works ok on Localhost

I have an ASP.NET Core 2.1 app that I've hosted in an Azure app service. When executed locally I'm able to access the controller. But when I host in an Azure app I receive a 404. Here are the minimal steps to reproduce.

In Visual Studio 2017 add a new project. Select ASP.NET Core Web Application. Select ASP.NET Core 2.1, API project template, no authentication, configure for HTTPS. Run the new app as a self hosted (not using IIS). Browse to https://localhost:5001/api/values. I get the expected response (although there is an exception on the command line about failure to authenticate HTTPS connection).

Right click the project and select publish. Select to create a new app service. I selected my existing subscription, hosting plan, and resource group. I left the default app name. Create the app.

Browse to the url https://app_name.azurewebsites.net and I see the default page. Browse to https://appname.azurewebsites.net/api/values and I get a 404.

I'm sure I'm missing something quite stupid, but I just can't figure it out.

like image 538
Brian Heilig Avatar asked Apr 12 '19 20:04

Brian Heilig


People also ask

How do I fix error 404 in web API?

A simple solution is to check for the HTTP status code 404 in the response. If found, you can redirect the control to a page that exists. The following code snippet illustrates how you can write the necessary code in the Configure method of the Startup class to redirect to the home page if a 404 error has occurred.

Does Azure support .NET core?

Azure App Service is a Microsoft cloud computing platform service for hosting web apps, including ASP.NET Core.


1 Answers

Similarly to the @StuartLC's answer, there's another development-time-only option to be wary of with Visual Studio's .NET Core Web API template:

I spent a very frustrating hour foolishly expecting a newly deployed Azure WebApp to show me the Swagger page for my API when I navigated to it in the browser. Instead I was just getting a 404. What I should have done was used Postman or whatever to check.

Only when I checked the code did I realise that the Swagger UI is only wired in at development time by default in the template:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseSwagger();
        app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication6 v1"));
    }
like image 96
tomRedox Avatar answered Oct 13 '22 02:10

tomRedox