Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing Question

I must be dense. After asking several questions on StackOverflow, I am still at a loss when it comes to grasping the new routing engine provided with ASP.NET MVC. I think I've narrowed down the problem to a very simple one, which, if solved, would probably allow me to solve the rest of my routing issues. So here it is:

How would you register a route to support a Twitter-like URL for user profiles?

www.twitter.com/username

Assume the need to also support:

  • the default {controller}/{action}/{id} route.

  • URLs like:

    www.twitter.com/login
    www.twitter.com/register

Is this possible?

like image 825
Kevin Pang Avatar asked Dec 18 '08 07:12

Kevin Pang


1 Answers

What about

routes.MapRoute(
    "Profiles",
    "{userName}",
    new { controller = "Profiles", action = "ShowUser" }
);

and then, in ProfilesController, there would be a function

public ActionResult ShowUser(string userName)
{
...

In the function, if no user with the specified userName is found, you should redirect to the default {controller}/{action}/{id} (here, it would be just {controller}) route.

Urls like www.twitter.com/login should be registered before that one.

routes.MapRoute(
    "Login",
    "Login",
    new { controller = "Security", action = "Login" }
);
like image 95
gius Avatar answered Nov 16 '22 02:11

gius