如何修复您的登录asp.net mvc以加载页面?

时间:2020-03-06 07:38:49

标签: c# asp.net-mvc

enter image description here

我的登录页面每次都无法启动。错误是

“ /”应用程序中的服务器错误。无法找到该资源。

我重新检查了以下文件RouteConfig.cs,并按照如下方式映射了路由,包括带有操作方法的控制器。现在的问题是,如何无法加载此页面?注册页面不会立即加载并检查。

// Controller class
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            TempData["ErrorMessage"] = "";
            TempData["LoginMessage"] = "";
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);

            }

            var emailExist = await UserManager.FindByEmailAsync(model.Email);
            if(emailExist != null)
            {
                if(emailExist.EmailConfirmed == false)
                {
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(emailExist.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = emailExist.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(emailExist.Id, "Confirm your account", ConfirmAccountMailBody(callbackUrl));

                    TempData["ErrorMessage"] = "Email id is not verified. Please check your email and verify account !!!";
                    ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                    return View(model);
                }
            }
            else
            {
                TempData["ErrorMessage"] = "Email is not registered !!!";
                ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                return View(model);
            }

            var loggedinUser = await UserManager.FindAsync(model.Email, model.Password);
            if(loggedinUser !=null)
            {
                await UserManager.UpdateSecurityStampAsync(loggedinUser.Id);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    TempData["ErrorMessage"] = "Email or Password is Incorrect";
                    ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                    return View(model);
            }
        }

// Route-config
   // Route to Login.
            routes.MapRoute(
               name: "Login",
               url: "login/",
               defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
           );

// Index.cshtml
@using eNtsaRegistrationTraining.Models
@model LoginViewModel
@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_LoginLayout.cshtml";
}


<div class="login-box">
    <div class="login-logo">
        <a href="@Url.RouteUrl("Login")"><b>eNtsa</b> Registration</a>
    </div>
    <!-- /.login-logo -->
    <div class="card">
        <div class="card-body login-card-body">
            <p class="login-box-msg">Sign in to start your session</p>
            @using (Html.BeginForm("Index", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @role = "form" }))
            {
                @Html.AntiForgeryToken()
                <div class="input-group mb-3">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control", type = "email", placeholder = "Email", autofocus = "autofocus", required = "required" })
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-envelope"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", type = "password", placeholder = "Password" })
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-lock"></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-8">
                        <div class="icheck-primary">
                            @Html.CheckBoxFor(m => m.RememberMe, new { @type = "checkbox", id = "remember" })
                            <label for="remember">
                                Remember Me
                            </label>
                        </div>
                    </div>
                    <!-- /.col -->
                    <div class="col-4">
                        <button type="submit" class="btn btn-primary btn-block">Sign In</button>
                    </div>
                    <!-- /.col -->
                </div>
            }

            <div class="social-auth-links text-center mb-3">
                <section id="socialLoginForm">
                    @*@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })*@
                </section>
            </div>
            <!-- /.social-auth-links -->

            <p class="mb-1">
                <a href="@Url.RouteUrl("ForgotPasswd")">Forgot your password?</a>
            </p>
            <p class="mb-0">
                <a href="@Url.RouteUrl("Register")" class="text-center">Register a new membership</a>
            </p>
        </div>
        <!-- /.login-card-body -->
    </div>
</div>

<div class="modal fade" id="modal-danger" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content bg-danger">
            <div class="modal-header">
                <h4 class="modal-title">Authentication Failed !</h4>
            </div>
            <div class="modal-body">
                @TempData["ErrorMessage"]
            </div>
            <div class="modal-footer justify-content-between">
                <button type="button" class="btn btn-outline-light" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function ShowErrorPopup() {
        $("#modal-danger").modal();
    }
</script>
@if (ViewBag.JavaScriptFunction != null)
{
    <script type="text/javascript">
            @Html.Raw(ViewBag.JavaScriptFunction)
    </script>
}

3 个答案:

答案 0 :(得分:0)

嗯,为什么不这样做呢?

 routes.MapRoute(
               name: "Login",
               url: "Account/Index",
               defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
           );

答案 1 :(得分:0)

您的路线具有“索引”页面,但您没有创建任何“索引(HTTP GET)”页面。这就是为什么会出现此错误。

答案 2 :(得分:0)

RouteConfig.cs

    /*routes.MapRoute(
                name: "Login",
                url: "login/",
                defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
            );
            */
            routes.MapRoute(
               name: "Login",
               url: "login/",
               defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
           );
  1. 删除了文件随附的Login.cshtml。注意这是具有个人帐户用户的MVC.ASP.NET
  2. 每次加载页面时,它都会获取映射路线并命中控制器。现在可以正常工作了。