Ad

Use The Routing Engine For Form Submissions In ASP.NET MVC Preview 4

- 1 answer

I'm using ASP.NET MVC Preview 4 and would like to know how to use the routing engine for form submissions.

For example, I have a route like this:

routes.MapRoute(
            "TestController-TestAction",
            "TestController.mvc/TestAction/{paramName}",
            new { controller = "TestController", action = "TestAction", id = "TestTopic" }
            );

And a form declaration that looks like this:

<% using (Html.Form("TestController", "TestAction", FormMethod.Get))
   { %>
     <input type="text" name="paramName" />
     <input type="submit" />
<% } %>

which renders to:

<form method="get" action="/TestController.mvc/TestAction">
  <input type="text" name="paramName" />
  <input type="submit" />
</form>

The resulting URL of a form submission is:

localhost/TestController.mvc/TestAction?paramName=value

Is there any way to have this form submission route to the desired URL of:

localhost/TestController.mvc/TestAction/value

The only solutions I can think of are to create a separate action that just checks the request parameters, or to use Javascript.

Ad

Answer

Solution:

public ActionResult TestAction(string paramName)
{
    if (!String.IsNullOrEmpty(Request["paramName"]))
    {
        return RedirectToAction("TestAction", new { paramName = Request["paramName"]});
    }
    /* ... */
}
Ad
source: stackoverflow.com
Ad