2

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!

2
  • There's some difference between asp.net web api and asp.net core web api You can refer to the doc here. learn.microsoft.com/en-us/aspnet/core/migration/… Commented Feb 4, 2019 at 11:55
  • 1
    Hi, thank you for your answer. It helped me in finding a solution to get it working for the app service :) I didn't know that I can access the scm through here (xxx.scm.azurewebsites.net). If im not wrong, installing this site extension (ASP.NET Core 2.2 (x64) Runtime) helps. Commented Feb 6, 2019 at 7:34

3 Answers 3

8

I know the answer has helped you resolved your issue. But I came across a similar situation. In my case my api is up and running but the Swagger Page I was expecting to see when I navigate to the https://api.mydomain.com is showing me error 404 not found, which made me assumed the app isn't working.

After several hours of tweaking and turning azure app service debugging and log streaming on and off. I noticed the hangfire jobs in my application were firing and working.

So I went back to check my code.

And here is pretty much what I did that made me assumed the app service wasn't working. Which infact was working pretty well.

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, UserManager<AppUser> userManager,
           RoleManager<IdentityRole> roleManager)
      {
           //Stripped Down for readability purpose

           app.UseSwagger();
           if (!env.IsProduction())
           {
                app.UseSwaggerUI(c =>
                {
                     c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API V1");
                     c.RoutePrefix = string.Empty;
                });
           }

           if (!env.IsProduction())
           {
                app.UseHangfireDashboard();
           }
           else
           {
                app.UseHangfireDashboard("/hangfire", new DashboardOptions
                {
                     Authorization = new[] { new HangfireAuthorizationFilter() }
                });
           }
           app.UseHangfireServer();
      }

So, in my case it was this line that made the app service return error 404

app.UseSwagger();
           if (!env.IsProduction())
           {
                app.UseSwaggerUI(c =>
                {
                     c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API V1");
                     c.RoutePrefix = string.Empty;
                });
           }

Because I only want the Swagger page to be displayed only when in test or development environment and not production. But the human being in me expected the swagger page to come up and display.

I hope this helps anyone who is in my situation as well.

Sign up to request clarification or add additional context in comments.

1 Comment

LOL I'm so embarrassed I had the same issue.. D'OH! thanks for having it first i guess?
3

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.

Comments

1

I guess your routing is not done.try with your Url+/api/xxx.

Example: https://xxxx.azurewebsites.net/api/xxx

Here https://xxxx.azurewebsites.net is your azure url and api/xxx is your launch url defined in launchSettings.json file like "launchUrl": "api/xxx/",

[Route("api/[controller]")] should be in your controller class.

It should work.

1 Comment

it works to me because i was trying xxxx.azurewebsites.net/[namesolution]/api/xxx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.