views:

70

answers:

1

In my ASP.NET MVC application, I want to use this ASP.NET MVC Attribute Based Route Mapper, first announced here.

So far, I understand how to implement code that uses it, but I've run into a few questions that I think those who have used this attribute-based route mapper in the past will be able to answer.

  • How do I use it with ActionResults that are for HTTP POSTs? In other words, how does it work with form submissions and the like? Should I just put the URL of the GET method in, or should I use the GET method URL without any parameters (as in HTTP POST they aren't passed in through the URL)?
  • How do I use it with "URL querystring parameters"? Can the attribute be configured to map to a route such as /controller/action?id=value rather than /controller/action/{id}?

Thanks in advance.

A: 

How do I use it with ActionResults that are for HTTP POSTs?

You decorate the action that you are posting to with the [HttpPost] attribute:

[Url("")]
public ActionResult Index() { return View(); }

[Url("")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

If you decide to give the POST action a different name:

[Url("")]
public ActionResult Index() { return View(); }

[Url("foo")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

You need to supply this name in your helper methods:

<% using (Html.BeginForm("foo", "home", new { id = "123" })) { %>

How do I use it with "URL querystring parameters"?

Query string parameters are not part of the route definition. You can always obtain them in a controller action either as action parameter or from Request.Params.

As far as the id parameter is concerned it is configured in Application_Start, so if you want it to appear in the query string instead of being part of the route simply remove it from this route definition:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoutes();
    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}
Darin Dimitrov
For the first part: does anything change if my GET route is something like `/controller/action/{param}` rather than just `""`? For the second part: Doesn't the default ASP.NET MVC routing control map all querystring parameters (not just `id`)?Thanks a lot for your help!
Maxim Zaslavsky
Darin Dimitrov