views:

334

answers:

3

I have two routes I want mapped in my ASP.NET MVC application

  1. /User/Login
  2. /User/{userid}/{username}/{action} (e.g. /User/1/blah/profile)

Here are the routes I've defined:

    routes.MapRoute(
        "Profile",
        "Users/{userID}/{username}/{action}",
        new { controller = "Users", action = "Profile" }
    );

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

This works great so far in most instances. The following URLs work from my home page:

<%= Html.ActionLink((UsersController x) => x.Login(), "Login") %>
<%= Html.ActionLink((UsersController x) => x.Profile(1, "blah") %>

These map to (respectfully):

/Users/Login /Users/1/blah

However, once I've navigated to /Users/1/blah, the login url immediately turns to /Users/1/blah/login. Any idea how to fix this?

A: 

Sorry but i just have to say.. Bad design?

Why do you need both username and userid in the url? why not have /Users/Profile/fekberg instead? The username should, imo be unique.

Filip Ekberg
You realize that this site does the same thing right? The username isn't used for indexing, but for SEO.
Kevin Pang
Well i'd still leave the user-id out of the question, but sure, it can be used :)
Filip Ekberg
A: 

is your route hitting an Authorize filter? Is there a requirement to be logged in to view the /Users/1/blah page? (ie. is there an [Authorize] attribute on the UsersController class, or on the Profile Action?)

well then, if it is not an Authorize filter, I highly suggest you implement this Routing Debugger Tool into your project.

E Rolnicki
No, anyone can view this page. I'm not sure what this has to do with the routing though. Or are you just curious?
Kevin Pang
it sounded as if you were being redirected to a login page when trying to hit a page which would typically be protected by an authorization filter.
E Rolnicki
+1  A: 

You want to use <%=Html.RouteLink%>

This is very similar to the problem I had which you can view here

qui