tags:

views:

635

answers:

3

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: http://myapp.com/plot/my-plot-name. 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:

[Authorize]
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.

+1  A: 

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.

Timothy Khouri
A: 

That helps, though it doesn't get me completely there. There seems to be two problems with it. 1) Building the ActionLink based on your example generates the following URL for the link: http://myapp.com/plot/ViewDetails?plotName=my-plot-name. Though, that did give me the example that I needed to get what I wanted from the ActionLink. Now, I can generate the link that I want by using:

Html.ActionLink( plot.Name, plot.UrlFriendlyName )

However, I feel like I'm missing something with my Route definition, because even when I can get the ActionLink to generate links using the URL format that I want, I run into problem number 2) when I actually click on the link, I get a 404 error "The resource cannot be found" - /plot/my-plot-name. Ssooo...is there something wrong with the way I have my route setup? I was under the impression that by defining a route that maps "/plot/{name}" to the "ViewDetails" action on the "Plot" controller, that that would be the method called from URLs with the format that I'm generating, but that doesn't appear to be happening. I must be missing something still.

Thank you for your assistance.

Bob Yexley
You must have done something incorrectly... did you add the "MapRoute" to your Global.asax? The link that was generated should have been http://blah/plot/my-plot-name... and the page should be found. If you're still having a prob, I'll upload a sample project.
Timothy Khouri
A: 

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

routes.MapRoute( 
    "PlotByName", 
    "plot/{name}", 
    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?

Pure.Krome