Reputation: 18065
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.
HTTP POST
s? 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)?/controller/action?id=value
rather than /controller/action/{id}
?Thanks in advance.
Upvotes: 2
Views: 2321
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