本文介绍了web api 2 路由 - 找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将 Web Api 控制器添加到 MVC 5 应用程序,但我一直收到错误 404 - 找不到资源.我已将 GlobalConfiguration.Configure(WebApiConfig.Register) 添加到 Application_Start()
I've added Web Api controller to MVC 5 application but all the time I get Error 404 - The resource cannot be found. I've added GlobalConfiguration.Configure(WebApiConfig.Register) to Application_Start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
我已经注册了路线
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
推荐答案
WebApi 路由在我将 Register api 方法的位置更改为 register 路由的上方后开始工作:
WebApi routing started to work after I've changed the position of Register api method to be above of register routes:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
这篇关于web api 2 路由 - 找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!