ASP.NET Web API not working on Azure

2019-06-17 19:08发布

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.

4条回答
在下西门庆
2楼-- · 2019-06-17 19:45

The same than @Matija Grcic, after deploying successfully a Web API application on Azure many times, I hit the wall of "The resource you are looking for has been removed.." in new deployments. I found the culprit in mixed versioning problems from the next three assemblies:

System.Net.Http.dll

System.Net.Http.Formating.dll

System.Net.Http.WebRequest.dll

I noted that my project reference to those assemblies was set to use them from global cache, and then were not copied to Azure in the deployment. But also I found that in the error Log of my application, it was complaining of not found them. Last clue: they existed in directory of my development machine.

I removed them of the development directory and Web.config any explicit mention of those assemblies from the section runtime>assemblyBinding>dependentAssembly. Published again and the error prevailed.

Then manually copy the referenced three assemblies to the bin folder in Azure using FTP and voila! My Web API is working!

I guess that there are new versions in the environment of Azure for new Web Apps (.Net 4.7 in the moment of write this) and my old Visual Studio project may been building MVC and Web API expecting to use the old versions. In that way, trying to create a new project using and referencing a newer .Net framework must help, like @Matija Grcic did it.

查看更多
smile是对你的礼貌
3楼-- · 2019-06-17 19:48

I have solved the problem by change from .net Framework 4.6.1 to 4.6.2. Now it works for me

查看更多
仙女界的扛把子
4楼-- · 2019-06-17 19:54

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.

查看更多
Explosion°爆炸
5楼-- · 2019-06-17 19:56

Old post, but have you tried:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
查看更多
登录 后发表回答