gene
gene

Reputation: 2108

Resource cannot be found

I'm new to MVC and trying to convert regular .NET Web application to MVC. I'm working on the login page.

As a template I'm using the default MVC application that came with installation.

I added my login form into the index page and changed the form action point to ActionController/Login route.

When clicking submit, I have an error indicating that resource /AccountController/Login cannot be found.

This is my index page:

@model Admin.Models.LoginModel

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title</h1>
            </hgroup>
        </div>
    </section>
}
<h3 class="logo">Title </h3>

<section id="loginForm">
@using (Html.BeginForm("Login","AccountController", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Please login</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </li>
        </ol>
        <input type="submit" value="Log in" />
    </fieldset>
}
</section> 

This is a part of an Action Controller:

[Authorize]
    [InitializeSimpleMembership]
    public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login

        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password))
            {
                return RedirectToLocal(returnUrl);
            }

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

What am I doing wrong in here? Seems everything is ok.

Upvotes: 0

Views: 340

Answers (2)

Awais Mahmood
Awais Mahmood

Reputation: 1336

In your view replace the Html.BeginForm with the following:

@using (Html.BeginForm("Login","Account", FormMethod.Post))

Upvotes: 1

Andrew Arace
Andrew Arace

Reputation: 457

You need to decorate your Login action with [HttpGet] to signify that it is a GET method.

edit: also, the POST method needs [HttpPost]

Upvotes: 0

Related Questions