MVC Attribute Routing - Default Controller Index with GET and POST
Asked Answered
S

1

6

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,

Songstress answered 7/2, 2014 at 15:25 Comment(0)
S
12

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!

Songstress answered 7/2, 2014 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.