Search code examples
asp.netazureasp.net-web-apiasp.net-core-2.1asp.net-web-api-routing

ASP.NET Core Web API runs locally but not on Azure app service


I have been following this link exactly (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio) to create my web api.

**Locally, it is working fine. Tested the links and it returned JSON data

However, once I deploy my web api up to azure app service, all my api links have been returning me error 404. Is there anything that I might have missed out for routing?

In my controller I have added this to my head.

[Route("api/xxx")]
[ApiController]

In each function, I have added this

[HttpPut("xxx/{Id}")]

As for my program/startup it is totally the same as the tutorial

  1. Program class

    public class Program
    {
       public static void Main(string[] args)
       {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
    
  2. Startup.cs

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseMvc();
    }
    

    Let me know if you need anymore information. Really appreciate any help thanks!


Solution

  • Setting the web.config as below:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Somerandomname.WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
    

    For more details, you could refer to this article. And here is Publish an ASP.NET Core Web API to an Azure App Services Web App you could follow.