1

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

1 Answer 1

2

You need to add a route to match that URL:

routes.MapRoute(null, "{controller}/edit/{action}/{id}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.