1

I want to add a custom route, but I can't get it to work. I want to use normal routing, except when it hits a specific controller,to use a different optional parameter instead of id.

In my area, this exists:

context.MapRoute(
     "Admin_default",
     "Admin/{controller}/{action}/{id}",
     new { action = "Index", id = UrlParameter.Optional }
     );

and above that I tried to add:

context.MapRoute(
           "Admin_Users",
           "Admin/Users/{action}/{username}",
           new { action = "Index", username = UrlParameter.Optional }
       );

And in code, I call the page with:

@Html.ActionLink("Edit", "Edit", new { username=user.UserName })

and it works, but the link shows up as /Admin/Users/Edit/?username

and I want: /Admin/Users/Edit/username

However, I want to keep the same route for all my other pages, that being:

/Admin/Shop/Products/Edit/1

which uses the default routing

EDIT

I got it to work by adding in controller="Users", in my route.

context.MapRoute(
       "Admin_Users",
       "Admin/Users/{action}/{username}",
       new { controller="Users", action = "Index", username = UrlParameter.Optional }
   );

I thought this was the point of the URL part (the line above) of it?

3
  • Please, stop referring to "ASP.NET MVC" simply as "MVC". One is a framework, while other is a language-independent design pattern. It's like calling IE - "the internet" Commented Jan 17, 2013 at 18:08
  • Dont worry, i used the aspnet.mvc tag. Any advice on the question? Commented Jan 17, 2013 at 18:37
  • ... actually i used the tag , if you look in the edit history Commented Jan 18, 2013 at 7:12

1 Answer 1

1

your code is correct, but your order is not.

ASP.NET MVC uses the first route that applies. In your case, the generic route will apply. Change the order and your 'Admin/User/...' route will have higher precedence.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that's the problem. It didn't work above or below, but I think it's because the default route works for everything under Admin/Users also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.