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:

            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" />

The resulting URL of a form submission is:


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


Thanks in advance for any assistance.

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

+2  A: 

In your route, get rid of the {paramName} part of the URL. It should be:


As that is the URL you want the request to route to. Your form will then post to that URL. Posted form values are mapped to parameters of an action method automatically, so don't worry about not having that data passed to your action method.

+1  A: 


Hmm thanks for that, but the problem is that the URL is then still of the form Action?param=value, not a nice RESTful URL. The other thing is that doing that means my links change to this form as well because of the new route.

I should clarify that this is when using GET. If I use POST I am going to get a URL which is just TestController.mvc/TestAction. This would mean that users that bookmark this page won't get the desired result when they come back.

What I want is a way to remap named parameters to the correct route and I guess a custom HTTPHandler will be the only way.


My understanding is that this is how HTML works. If you do a and post the form, then the form will post foo?param1=value1&...&paramn=valuen. It has nothing to do with MVC.

Besides, what part of REST does that URL violate? It's not a "pretty" URL, but by strict definition of REST, it can be RESTful. REST doesn't specify that query parameters have to be in an URL segment. And in this case, those are query parameters.

Yeah you're right - thanks. Reflecting on this I'm not even sure what I was thinking - maybe just to get the MVC app to route to a nice new URL after form submission. The later GET/POST attributes help things.
+4  A: 


public ActionResult TestAction(string paramName)
    if (!String.IsNullOrEmpty(Request["paramName"]))
        return RedirectToAction("TestAction", new { paramName = Request["paramName"]});
    /* ... */
This makes 2 requests to the server instead of one, right?