tags:

views:

486

answers:

2

I could have SWORN I had this working, but somewhere along the line I apparently broke it. Maybe it was during my migration from ASP.NET MVC in VS2008 to ASP.NET MVC2 in VS2010.

My ActionLink:

Html.ActionLink(segment.TitleWithChapterNumber, "Index", "Read", new { bookCode = Model.Product.Id, segmentCode = segment.Index }, null)

The route I expect it to match:

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);

This renders a link that looks like: http://localhost/Obr/Read?bookCode=14&segmentCode=0 But I want it to look like http://localhost/Obr/Read/14/0 Clicking the link that it renders does take me to the right controller and the response is accurate. If I paste in the link I WANT it to look like, it does work. I guess it's just not matching?

Am I missing something obvious? I've stared at it so long I don't even know what I am looking for anymore.

For reference, here are ALL of my routes:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "ReadImage",
    "Read/Image/{bookId}/{imageName}",
    new { controller = "Read", action = "Image" }
);

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);

routes.MapRoute(
    "BookReport",
    "BookReport/{action}/{folder}",
    new { controller = "BookReport", action = "Details", folder = "" }
);

routes.MapRoute(
    "Reference",
    "Reference/Details/{referenceType}/{searchText}",
    new { controller = "Reference", action = "Details", referenceType = "", searchText = "" }
);

routes.MapRoute(
    "PaginatedAudits",                                      // Route name
    "Audit/Page/{pageNumber}",                              // URL with parameters
    new { controller = "Audit", action = "Index" }          // Parameter defaults
);

routes.MapRoute(
    "PaginatedReadLog",                                     // Route name
    "ReadLog/Page/{pageNumber}",                            // URL with parameters
    new { controller = "ReadLog", action = "Index" }        // Parameter defaults
);

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

The action signature looks like this:

[Authorize]
public ActionResult Index(string bookCode, string segmentCode, string sectionCode, string renderStrategy)
{
    // code
}
A: 

Try a route link by explicitly giving the name of the route:

Html.RouteLink(
    segment.TitleWithChapterNumber, // linkText
    "Read", // routeName
    new { 
        bookCode = Model.Product.Id, 
        segmentCode = segment.Index 
    }, // routeValues
    null // htmlAttributes
)
Darin Dimitrov
For some reason that generated an anchor that linked to the page I was already on, which happened to be `http://localhost/Obr/Library/Details/14`
Chris
A: 

Your route definitions should have UrlParameter.Optional instead of empty strings.

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = UrlParameter.Optional, segmentCode = UrlParameter.Optional, sectionCode = UrlParameter.Optional, renderStrategy = UrlParameter.Optional }
);

This will also help with redirect in controllers and form urls created through MVC extensions.

Wiaan