问题
I have been following this link exactly (https://docs.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
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>(); }
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!
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/54515508/asp-net-core-web-api-runs-locally-but-not-on-azure-app-service