Eduardo Fonseca
Eduardo Fonseca

Reputation: 575

How to make ASP.NET Core Web API on .NET 5 run on Azure?

I am having an issue when trying to publish a Web API running on .NET 5 on Azure I keep getting a 404 error

Even the default ASP.NET Core Web API template is failing when deployed to Azure.

I already set Azure to use early access on .NET 5 and I even tried self-contained deployment.

Ignore the fact the screenshot is accessing index.html, that was just a test, same happens accessing the routes of any actions in the controllers.

Any ideas?

Upvotes: 0

Views: 454

Answers (3)

Eduardo Fonseca
Eduardo Fonseca

Reputation: 575

As of yesterday the issue is not happening anymore

Upvotes: 0

Eduardo Fonseca
Eduardo Fonseca

Reputation: 575

So, I was able to fix the issue. I compare to other project I had deployed in the past and to a Blazor in .NET 5 I have deployed too. Turns out Azure required me to add an additional wwwroot with js, and css files, even when my project is configured to use endpoints only, and is not using static files at all.

Upvotes: 0

Fei Han
Fei Han

Reputation: 27835

Please note that the API template is a project template for creating a RESTful HTTP service, as @Andy mentioned, WebAPI project usually does not serving HTML pages.

If you really want to serve a index.html page in your WebAPI project, you can try:

1)create a wwwroot folder in your project and put index.html within it

enter image description here

2)call the UseStaticFiles method in Startup.Configure, which enables static files to be served

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...
    //your code here...

    app.UseHttpsRedirection();

    app.UseStaticFiles();

    app.UseRouting();

    //...

For more information about serving static files, please check:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0

it happened even with the default route, no static files.

You can try to access your API endpoint with your actual route that you configured, that might look like below.

https://xxx.azurewebsites.net/api/{controller_name}/{action_name?}

Or

https://xxx.azurewebsites.net/{controller_name}/{action_name?}

Upvotes: 2

Related Questions