2017-10-16 100 views
3

我正在实现一些东西,我希望在更传统的MVC实现旁边放置web api。在不同命名空间中使用重复的控制器名称的路径模板

的结构是这样的:

+ Controllers 
    + Web 
    - Product.cs 
    + Api 
    - Product.cs 

在我的代码,我想路由通过/api进来的Api命名空间的所有请求,以及其他一切的Web命名空间,像:

// Want to indicate that these should all choose the the Api namespace 
routes.MapRoute(
    name: "api_route", 
    template: "api/{controller}/{action}/{id?}"); 

// Indicate that these should all choose the from the Web namespace. 
routes.MapRoute(
    name: "default_route", 
    template: "{controller}/{action}/{id?}"); 

据我所见,没有惯用的方式来指示哪个命名空间可供选择。有一个更好的方法吗?或者我需要手动指定每个控制器的路线?

编辑: 因为它似乎这可能是一个有争议的问题,如果使用Razor视图。无论如何,我会留下来看看是否有人有答案。

回答

0

由于Owin/Katana可能使用app.Map来隔离请求管道。 我使用这两个启动配置来处理这些情况:

1)使用Owin Startup文件在/api的主机WebApi。

app.Map("/api", builder => 
{ 
    var config = new HttpConfiguration(); 
    builder.UseWebApi(config); 
}); 

2)从路径集合中的MVC省略/api(设定在Global.asax中的启动时间)

RouteTable.Routes.MapMvcAttributeRoutes(); 
RouteTable.Routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional }); 

// Explicitly tell this route to be solely handled by the Owin pipeline. 
RouteTable.Routes.MapOwinPath("/api"); 
相关问题