I have a controller called HotelsController
to insert and edit hotels.
It has the following setup (method implementation removed for simplicity):
[RoutePrefix("{member_id:int}/hotels")]
public class HotelsController : ApplicationController
{
[Route("delete/{id:int}", Name = NamedRoutes.HotelDelete)]
public ActionResult Delete(int id)
{
}
[Route("new", Name = NamedRoutes.HotelNew)]
public ActionResult New()
{
}
[HttpPost]
[ValidateInput(false)]
public ActionResult New(HotelDataEntry hotel)
{
}
[Route("edit/{id:int}", Name = NamedRoutes.HotelEdit)]
public ActionResult Edit(int id)
{
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(HotelDataEntry hotel)
{
}
}
As you can see the following routes are using attribute routing:
- Delete
- New (without parameters)
- Edit (without parameters)
The following routes use no attribute routing:
- New (with parameters)
- Edit (with parameters)
The routing is setup in Global.asax.cs as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
Routen.Standard.ToString(),
"{member_id}/{controller}/{action}/{id}",
new { action = "browse", id = UrlParameter.Optional },
new { id = AllowedIdsRegExOptional }
);
}
Problem: Attribute routing works. I can call the Edit action with http://localhost:54868/301011/hotels/edit
but the form on that page should post to the same uri and call the action that uses no attribute routing. But instead the action using the attribute based routing is called again. Why?
The form is supplied with method="post"
. Do you have any idea why the convention based route is not used? Thank you for your help.
Edit: I tried to add [HttpGet]
in front of the attribute-routed New and Edit actions. The result is that on posting the form ASP.NET shows an error that the route is invalid. So for some reasons, the convention based routing is not working on the controller.