ASP.NET MVC Beta Routes, Controller Actions, Parameters and ActionsLinks...putting it all together

I'm having some trouble with ASP.NET MVC Beta, and the idea of making routes, controller actions, parameters on those controller actions and Html.ActionLinks all work together. I have an application that I'm working on where I have a model object called a Plot, and a corresponding PlotController. When a user creates a new Plot object, a URL friendly name gets generated (i.e.). I would then like to generate a "List" of the Plots that belong to the user, each of which would be a link that would navigate the user to a view of the details of that Plot. I want the URL for that link to look something like this: I've attempted to make that happen with the code below, but it doesn't seem to be working, and I can't seem to find any good samples that show how to make all of this work together.

My Route definition:

routes.MapRoute( "PlotByName", "plot/{name}", new { controller = "Plot", action = "ViewDetails" } );

My ControllerAction:

public ActionResult ViewDetails( string plotName )
    ViewData["SelectedPlot"] = from p in CurrentUser.Plots where p.UrlFriendlyName == plotName select p;
    return View();

As for the ActionLink, I'm not really sure what that would look like to generate the appropriate URL.

Any assistance would be greatly appreciated.


The answer is pretty simple: You have to supply enough values in your "ActionLink" that will fulfill your Route. Example:

<%= Html.ActionLink("Click Here", "ViewDetails", "Plot", new { name="my-plot-name" }, null)%>

If you leave out the "name=" part of the ActionLink method, then the RouteEngine won't see this link as being good enough to "match"... so then it would go to the default route.

This code above will make the URL look the way you want it.

How about this code-fix? (Note the name = null, appened to the end of the 4th line....)

    new { controller = "Plot", action = "ViewDetails", name = null }

and this should be renamed.. (notice plotName is renamed to name)

public ActionResult ViewDetails(string name ) { ... }

does that help?

Need Your Help

Company email or Domain name based email address validation in Yii

yii yii-components

Some times Yii Email validation show error message when we use domain name based email or company email address. how to fix it in yii framework?

Coded unit test, database and Dependency Injection

.net entity-framework unit-testing nunit

I have following questions related to the code unit tests for the classes which are pretty much integrated with the database logic. E.g. My class method calls another class that executes a database...