Click here to Skip to main content
15,921,226 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I am making an application in MVC3. IO have implemented the functionality that if user type after URL his/her username then he'll be redirected to the profile page

for eg if he types http:\localhost:4341\username

then he'll be redirected to http:\localhost:4341\users\showprofile\username

and if he entered an invalid username he'll be redirected to page not found http:\localhost:4341\users\pagenotfound

This is working fine, however if a user types the name of any controller the he is also redirecting to pagenot found, i want to redirect him to the controller's index page in this case

for eg if he entered Discussioncontroller's name http:\localhost:4341\Discussion

he should be redirected to http:\localhost:4341\Discussion

How can i implement this?

This is my route

C#
routes.MapRoute(
                  "Default",
                  "{id}",
                  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
              );

and on Home's Index i have make a condition like

C#
if (id != null)
                return RedirectToAction("ShowProfile", "User", new { @id = id });

            return View();
Posted
Updated 25-Jan-12 23:21pm
v2

1 solution

Your current routing strategy is:
1. It takes an optional Id only "{id}", in the url
2. It always defaults to Home controller, Index action.
Anything will match this condition as long as it has one item, you app thinks that discussion in http:\localhost:4341\Discussion is a username because it matches.
To fix :

routes.MapRoute(
    "Discussion", 
    "Discussion/{id}",                 
    new { controller = "Home", action = "Discussion", id = UrlParameter.Optional } //or whatever you need);

routes.MapRoute(
    "Default",  
    "{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });


The order of the Mappings is important, reverse these and it will match "Discussion" as a username before it reaches the mapping intended for it.

But I'd be wary of this strategy on a reasonably large application. Assuming you still want the default "{controller}/{action}/{id}" with defaults on controller(e.g. Home), and action (e.g. Index). The first stab might be:

routes.MapRoute(
    "Discussion", 
    "Discussion/{id}",                 
    new { controller = "Home", action = "Discussion", id = UrlParameter.Optional } //or whatever you need);

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}",                 
    new { controller = "Home", action = "index", id = UrlParameter.Optional } //or whatever you need);

routes.MapRoute(
    "User",  
    "{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

This seems reasonable: you want to check for urls specifying the discussion first, as they'd match both the "standard" and "user" patterns. We cannot put the "User" route before the "Default" as it matches any URL passed in the format http://localhost:4341/Controller/ . But, equally the default route will match http://localhost:4341/Username/ , interpreting username as a controller, (and attempting to access its Index action by default). You can potentially constrain the parts of the url (see http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs[^]) to do what you want, but you may want to think about the need to pass just the username into the URL instead, depending upon your requirements. Even just changing to "/Users/username" could make things much easier.

Hope this helps & is clear: it seems difficult to explain!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900