Why am I getting this 404 error?

In newly created MVC4 app, insert this function into your account controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AdminLogin(AdminLoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login("administrator", model.Password, persistCookie: model.RememberMe))
        {
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The password provided is incorrect.");
        return View(model);
    }

      

and this one

public class AdminLoginModel
{ 
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

      

is placed in accountModel.cs. I also created a new AdminLogin.cshtml file and left it blank. In the _loginPartial.cshtml file paste the action link

<li>@Html.ActionLink("Register", "AdminLogin", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>

But when I click on the Register link, I see a 404 error stating that it was /Account/AdminLogin

not found.

I miss putting in this tiny mvc; Could you help me? I am starting mvc.

+2


source to share


1 answer


Clicking the link in the browser results in a GET request, but your action method is only available for POST requests.

Add an attribute [HttpGet]

or remove an attribute [HttpPost]

to resolve this issue.



In general, you will want to keep using POST requests when submitting data. So my recommendation would be to change the client side to use the form (or use client side logic to intercept the click action of the link and submit the data with an ajax request).

+3


source







All Articles