文章地址:How do you route from an MVC project to an MVC ApiController in another project?
文章地址:How to Use MVC Controller and WebAPI Controller in same project
You need to register the routing for web api BEFORE registering the routing for MVC, so basically your App_Start()
function should look like this:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd BundleConfig.RegisterBundles(BundleTable.Bundles); }
需要知道的知识点:
1,mvc项目你需要在Global.asax.cs文件Application_Start方法中注册路由;
2,mvc路由和api路由是两种不一样的路由;
mvc路由一般在App_Start文件夹下RouteConfig.cs文件中;
api路由在App_Start文件夹下WebApiConfig.cs文件中;
文章:How do you route from an MVC project to an MVC ApiController in another project?
很好的介绍了如何在mvc项目中使用apiController,即需要配置api路由。
api路由类型是:MapHttpRoute
,mvc路由类型是:MapRoute
。
但是最后两种路由都在集合:Routes中。api路由需要配置在mvc路由之前。
同时路由还有命名空间限制,需要注意!