We have an MVC 5.1 project and are using attribute routing. Everything is working fine except the default page which has a login form on it.
[RoutePrefix("Home")]
public class HomeController : BaseController
{
[Route("~/")]
[Route]
[Route("Index")]
[HttpGet]
public ActionResult Index()
{
var model = new LoginViewModel();
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String Username, String Password)
The form is displayed via the GET fine but upon the POST we get...
HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
Normally the default route would handle both the POST and GET fine.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{dealerId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Obviously I am missing something here on the routing for the post on the default route as subsequent posts on other pages work fine.
Has anyone done this?
Thanks,
ASP.NET MVC offers two approaches to routing: The route table, which is a collection of routes that can be used to match incoming requests to controller actions. Attribute routing, which performs the same function but is achieved by decorating the actions themselves, rather than editing a global route table.
The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home.
Ok seems all I have to do is add
[Route("~/")]
[Route]
[Route("Index")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String Username, String Password)
Obvious really! Long Day!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With