2014-12-19

MVC Not Finding HttpPost Method

I have a MVC Web Form that when I posted, it was not finding the method decorated with the [HttpPost] attribute.

There in the form .cshtml page there was code like this:
@using (Html.BeginForm("Login", "Account"))
{
}

And code like this in the Account Controller

// Finding this
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
//...
}

// Wasn't finding this
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Login(LoginViewModel model, string returnUrl)
{
//...
}

When the form posted, it was going to the "GET" method instead of the "POST" method. After a bunch of debugging I finally figured out that the reason why is that I have an URL Rewrite rule on the web server that converts all URLs to lowercase for Search Engine Optimization (SEO) and logging consistency. This can also be done in the Web.Config file.

Because the form URL is in mixed case, it wasn't finding the correct method. All it takes to fix the problem is change the .cshtml form to have the parameters in lower case. Like this:

@using (Html.BeginForm("login", "account"))
{
}

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.