Sunday, March 6, 2011

ASP.NET MVC route question

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?

From stackoverflow
  • 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.

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

    Kevin Pang : No, anyone can view this page. I'm not sure what this has to do with the routing though. Or are you just curious?
    E Rolnicki : 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.
  • You want to use <%=Html.RouteLink%>

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

0 comments:

Post a Comment