user49126
user49126

Reputation: 1863

ASP.NET MVC Custom route logic

My custom route logic is redirecting /controller/edit/someaction to /controller/someaction. It works for urls in this format

/test/edit/delete?id=2

but not in this

/test/edit/delete/2

What can be the problem ?

Route logic

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}");  

Upvotes: 1

Views: 591

Answers (1)

SLaks
SLaks

Reputation: 887285

You need to add a route to match that URL:

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

Upvotes: 2

Related Questions