views:

588

answers:

1

Is there a way to force use of a named route in ASP.NET MVC when using Form.Begin. I'm learning more and more about routing and getting scared that it can be very fragile if you just change the order or whether or not parameters have defaults.

<% Form.Begin(...) %> <!-- no overload for providing a route name --%>

There does not seem to be an overload for a named route for beginning a form so instead the best I could come up with was this :

<form action="/Products/Command/ViewProduct" method="post">

I'm wondering if this missing overload is an oversight (Beta right now), if there is a reason for it or an alternative way to generate the URL.

I tried to use RouteLink and embed it in the Form tag, but RouteLink creates me the full HTML for an <A> tag which is no good.

action="<%= Ajax.RouteLink("Update Status", "product-route-short", new { action = "GetStatus", sku = "" }, new AjaxOptions { UpdateTargetId = "status" })%>"

What alternatives do i have to generate a URL from a named route.

Should I report this missing overload as an issue?

+1  A: 

If you need to generate a URL to a named route, you can use Url.RouteUrl(), you just have to put the controller and action into the route values object like so:

<%= Url.RouteUrl("NamedRoute", new { controller="Controller", action="Action", foo="Bar" ... }) %>

As for adding the overload, a lot of these helpers have so many different overloads that they start to conflict and become ambiguous. For example, consider the following overloads

public string BeginForm(string actionName, string controllerName)
public string BeginForm(string actionName)
public string BeginForm(string routeName, string actionName) // Uh oh!

The 1st and 3rd have identical signatures, so they are invalid.

You could always create your own form helper as an extension method to HtmlHelper if you need to use Named Routes often.

anurse