ASP.NET MVC自定义路由逻辑

时间:2011-12-29 20:39:40

标签: asp.net-mvc routing

我的自定义路由逻辑是将/ controller / edit / someaction重定向到/ controller / someaction。它适用于此格式的网址

/test/edit/delete?id=2

但不在此

/test/edit/delete/2

可能是什么问题?

路线逻辑

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// empty url is mapping to Home/Index
routes.MapRoute(null, "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

// accepts "Whatever/Edit/number", where Whatever is controller name (ie Home/Edit/123)
routes.MapRoute(null,
      // first is controller name, then text "edit" and then parameter named id
      "{controller}/edit/{id}", 
      // we have to set action manually - it isn't set in url - {action} not set
      new { action = "edit"},  
      new { id = @"\d+" }      // id can be only from digits
    );


// action name is AFTER edit (ie Home/Edit/MyActionMethod)
routes.MapRoute(null, "{controller}/edit/{action}");

// default action is index -> /Home will map to Home/Index
routes.MapRoute(null, "{controller}", new{action="Index"}); 

// accepts controller/action (Home/Index or Home/Edit)
routes.MapRoute(null, "{controller}/{action}");                 

// controller_name/action_name/whatever, where whatever is action method's id parameter (could be string)
routes.MapRoute(null, "{controller}/{action}/{id}");  

1 个答案:

答案 0 :(得分:2)

您需要添加与该网址匹配的路线:

routes.MapRoute(null, "{controller}/edit/{action}/{id}");