【发布时间】:2014-01-07 07:25:31
【问题描述】:
如果 Web API 在本地主机上运行,为什么它会停止在 Azure 网站上运行。
错误是加载资源失败:服务器响应状态为 404(未找到) 或 您要查找的资源已被删除,名称已更改,或暂时不可用。在浏览器中将 URL 粘贴到 api 时。
注意: 我从来没有遇到过这个错误,而且我已经有几个使用 Web Api 属性路由的网站。
网络配置
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
路由配置
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 显示这是一个常见问题:
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
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?
更新
在尝试了上面链接中建议的所有方法后,我从解决方案中删除了所有 *.dll,创建了一个新的 MVC+Web API 项目并将 *.dll 添加到解决方案中。首先,构建和一切都按预期工作。
【问题讨论】:
-
您要访问的 URL 是什么?
-
你也应该启用 CORS。
-
@ThiagoCustodio 这里不需要 CORS。
-
我有一些问题,但仅限于某些控制器上的某些方法。很奇怪的事
标签: asp.net-mvc azure asp.net-web-api