views:

31

answers:

1

I have defined a series of Routes in Global.asax.cs:

   public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(null, "", // Only matches the empty URL (i.e. ~/)
                        new
                        {
                            controller = "Stream",
                            action = "Entry",
                            streamUrl = "Pages",
                            entryUrl = "HomePage"
                        }
        );

        routes.MapRoute(null, "{streamUrl}", // matches ~/Pages
                        new { controller = "Stream", action = "List" }
        );

        routes.MapRoute(null, "{streamUrl}/{entryUrl}", // matches ~/Pages/HomePage
                        new { controller = "Stream", action = "Entry" }
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

When I put in mydomain.com/ or mydomain.com/Pages/HomePage the route works exactly as I expect. So now I'm writing a partial view to generate a list of links. as a test I put this code in the partial view:

<ul>
<% foreach (var item in Model) { %>

        <li id="<%:item.Text.Replace( " ", "-") %>">

            //This works - link shows up in browser as mydomain.com/
            <%: Html.RouteLink(item.Text, new{ streamUrl = "Pages", entryUrl = "HomePage" }) %>

            //This does not work - link shows up as mydomain.com/Blog?entryUrl=BlogEntryOne
            //instead of mydomain.com/Blog/BlogEntryOne
            <%: Html.RouteLink(item.Text, new{ streamUrl = "Blog", entryUrl = "BlogEntryOne" }) %>

        </li>

<% } %>
</ul>

I'm not sure why the entryUrl route value isn't being registered correctly. What am I missing?

+1  A: 

I'm not terribly accustomed with MVC, but I think you should put your more specific route first, as in:

routes.MapRoute(null, "{streamUrl}/{entryUrl}", // matches ~/Pages/HomePage
                new { controller = "Stream", action = "Entry" }
);

routes.MapRoute(null, "{streamUrl}", // matches ~/Pages
                new { controller = "Stream", action = "List" }
);
Dan Dumitru
Oh geez ... I can't see the forest through the trees. Thanks!
quakkels