ASP.NET MVC Пользовательская логика маршрута - PullRequest
1 голос
/ 30 декабря 2011

Моя пользовательская логика маршрута - это перенаправление / controller / edit / someaction в / controller / someaction. Это работает для URL в этом формате

/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 Ответ

2 голосов
/ 30 декабря 2011

Вам необходимо добавить маршрут, соответствующий этому URL:

routes.MapRoute(null, "{controller}/edit/{action}/{id}");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...