Asp.Net 4.6 如何给控制器添加路由前缀
Asp.Net 4.6 How to add a route prefix to controllers
我需要在一些控制器之前添加一个 'pathname' 以阐明它们的用途。
例如,目前:
/news/create
,
/news/edit
,
/event/create
,
/event/edit
,
我希望他们的路由器可以
/request/news/create
,
/request/news/edit
,
/request/event/create
,
/request/event/edit
我尝试在RouteConfig.cs
中加入如下代码
routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
控制器之一EventController.cs
public class EventController : Controller{
[HttpPost]
public ActionResult Edit(int id, Events eventModel){
//code
}
[HttpPost]
public ActionResult Create(Events eventModel){
//code
}
}
这里遇到一个问题:
/request/event/create
404错误
/request/event/edit/1
有效!
/event/create
有效!
/event/edit/1
有效!
那么,我的问题是为什么 /request/event/create
不起作用以及如何解决这个问题?
据我所知,在 Create 操作上面添加 [Route("request/event/create")]
可以使它起作用。
------------更新--------
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
}
注册路线的顺序很重要。将更具体的路线移到一般路线之前
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);
}
}
我需要在一些控制器之前添加一个 'pathname' 以阐明它们的用途。
例如,目前:
/news/create
,
/news/edit
,
/event/create
,
/event/edit
,
我希望他们的路由器可以
/request/news/create
,
/request/news/edit
,
/request/event/create
,
/request/event/edit
我尝试在RouteConfig.cs
中加入如下代码 routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
控制器之一EventController.cs
public class EventController : Controller{
[HttpPost]
public ActionResult Edit(int id, Events eventModel){
//code
}
[HttpPost]
public ActionResult Create(Events eventModel){
//code
}
}
这里遇到一个问题:
/request/event/create
404错误
/request/event/edit/1
有效!
/event/create
有效!
/event/edit/1
有效!
那么,我的问题是为什么 /request/event/create
不起作用以及如何解决这个问题?
据我所知,在 Create 操作上面添加 [Route("request/event/create")]
可以使它起作用。
------------更新--------
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
}
注册路线的顺序很重要。将更具体的路线移到一般路线之前
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Api",
url: "request/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);
}
}