问题描述
如果Web API可以在localhost上运行,为什么它会在Azure网站上停止工作.
Why would Web API stop working on Azure Websites if it works on localhost.
错误是无法加载资源:服务器的状态为404(未找到)或您正在查找的资源已被删除,名称已更改或暂时不可用.在浏览器中将URL粘贴到api时.
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.
注意:我从未遇到此错误,并且已经使用Web Api属性路由在Azure上建立了多个站点.
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发现这是一个常见问题:
Goggling revealed that this is a common problem:
在网络中找不到HTTP 404页面IIS 7.5中托管的Api
在Windows Azure网站上为ASP.NET Web API配置IIS方法
ASP Azure中的.NET MVC 4和Web API-找不到HTTP资源
更新
尝试了上面链接中建议的每种方法后,我从解决方案中删除了所有* .dll,创建了一个新的 MVC + Web API 项目,并将* .dll添加到了解决方案中.首先,构建和所有工作均按预期进行.
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.
推荐答案
旧帖子,但您是否尝试过:
Old post, but have you tried:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
这篇关于ASP.NET Web API在Azure上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!