views:

149

answers:

3

I'm trying to create a route for the following urls: www.mysite.com/user/username www.mysite.com/user/username/pictures

I tried doing that with the following code:

routes.MapRoute(
            "UserProfile",
            "user/{sn}/{action}",
            new { controller = "User", action = "Index", sn = "" }
        );

So if an action is not specified, you go to the index action.

However, it's not working and I'm not sure what I'm doing wrong.

Thanks for any help.

A: 

I think you will find your answer here: http://stackoverflow.com/questions/371938/aspnet-mvc-route-question

Good Luck!


Edit :

Ok guess you didnt really see the answer directly but it's there, however here is another resource: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

This actually explains how the routes work. Your probelm is that you will get // when sn is empty, hence: Users//Index which is incorrect.

Filip Ekberg
If you look at the code, there's nothing wrong with it. I don't mind getting an error if the sn is empty. However, I don't see why it won't work if it's not empty. (e.g. http://localhost/user/username doesn't work on my dev)
rksprst
Are you trying to point out that the MVC framework test for this and disallows any routes like the one I created?
rksprst
No /user/username doesnt work, because the route says: /user//username which is an incorrect url afaik. Set sn to 0 as standard and try it out.The route should work after that. Im just trying to point out that others have done this, and if you'd read what i posted, you'd see how.
Filip Ekberg
The way others have done is is with /user/profile/username ... I'm trying to do it with /user/username and the point of my question was to see if it was possible to do it that way.
rksprst
+1  A: 

Looks like your code is correct.

The order of the rules is important. Try to place this above all other rules. And if it will intefere with other rules, you should provide some constraints for the best matches.

maxnk
+1  A: 

I agree with maxnk, the code looks correct, it's probably just an ordering thing. I'd suggest checking out the Route Debugger that Phil Haack wrote: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx. It's very useful for these tricky route-ordering issues

anurse