ASP.NET Web API not working on Azure

Viewed 9473

Why would Web API stop working on Azure Websites if it works on localhost.

The error is Failed to load resource: the server responded with a status of 404 (Not Found) or The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. when pasting URL to api in browser.

NOTE: I have never encountered this error and I already have several sites already on azure using Web Api attribute routing.

WebConfig

 public static void Register(HttpConfiguration config)
        {

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
}

RouteConfig

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
             name: "Cities",
             url: "Cities/{id}/{name}/{action}",
             defaults: new { controller = "Cities", action = "Index" }
           );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );         
        }

Global.asax

 AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

Goggling revealed that this is a common problem:

HTTP 404 Page Not Found in Web Api hosted in IIS 7.5

WebAPI DELETE request return 404 error in Azure

WebApi and ASP.NET 4 routing issues

https://stackoverflow.com/questions/15805471/using-windows-azure-websites-with-extensionlessurlhandler

Configuring IIS methods for ASP.NET Web API on Windows Azure Websites

How Extensionless URLs Are Handled By ASP.NET v4

Getting 404 from WebAPI on Windows Azure Web Sites

ASP.NET MVC 4 and Web API in Azure - No HTTP resource was found

MVC 4.5 Web API Routing not working?

UPDATE

After trying every method suggested in the links above i've removed all the *.dlls from the solution, created a new MVC+Web API project and added *.dlls to the solution. Build and everything worked as expected in the first place.

5 Answers

Try the below 2 methods.

Method 1. try 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 ASP.NET Core Web API runs locally but not on Azure app service.

Method 2. Make sure your url in the browser is correct. In some occations you need to add

azureAPIurl/api/controllername

Make sure that you don't have any files that are accidentally excluded from your project - specifically check for Global.asax and Global.asax.cs. Excluded files don't get published, and therefore, would work locally but not on Azure.

Related