ASP.NET MVC Beta Routes, Controller Actions, Parameters, and Links Actions ... all together

I'm having problems with ASP.NET MVC Beta and the idea of ​​creating routes, controller actions, parameters for these controller actions and Html.ActionLinks works together. I have an application that I am working on where I have a model object called Plot and a corresponding PlotController. When the user creates a new Plot object, a friendly url ( ie ) is created. Then I would like to create a "List" of graphs owned by a user, each of which will be a link that will direct the user to view the details of that graph. I want the url of this link to look something like this: http://myapp.com/plot/my-plot-name... I tried to do it with the code below, but it doesn't seem to work and I can't find good samples that show how to make it all 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 far as ActionLink is concerned, I'm not entirely sure what it would be like to create an appropriate url.

Any help would be greatly appreciated.

+1


source to share


2 answers


The answer is quite simple: you need to provide enough values ​​in your ActionLink to execute your route. Example:

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

      



If you don't specify the "name =" part of the ActionLink method, the RouteEngine won't see that this link is good enough to "match" ... so it will fall back to the default route.

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

+1


source


How about this code? (Note the name = null appended to the end of the 4th line ....)

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

      

and this should be renamed .. (note.Name of title renamed to name)



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

      

Does it help?

0


source







All Articles