Maxim Zaslavsky
Maxim Zaslavsky

Reputation: 18065

Using the ASP.NET MVC Attribute Based Route Mapper

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.

Thanks in advance.

Upvotes: 2

Views: 2321

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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" }
    );
}

Upvotes: 1

Related Questions