简体   繁体   English

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

[英]How to fix your login asp.net mvc to load the page?

在此处输入图片说明

My Login page is failing to launch each time.我的登录页面每次都无法启动。 The error is错误是

Server Error in '/' Application. “/”应用程序中的服务器错误。 The resource cannot be found.无法找到该资源。

I rechecked the following files RouteConfig.cs and did map the routes as following including controller with action method.我重新检查了以下文件 RouteConfig.cs 并按照以下方式映射了路由,包括带有操作方法的控制器。 The question is now, how can this page not load?现在的问题是,这个页面怎么加载不出来? The register page does load and checked just now.注册页面刚刚加载并检查。

// 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>
}

Hmm, why not to do this?嗯,为什么不这样做呢?

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

Your route has Index page but you didnot make any Index(HTTP GET) page.您的路线有索引页面,但您没有制作任何索引(HTTP GET)页面。 That's why this error is coming.这就是为什么会出现此错误的原因。

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. Deleted the Login.cshtml that came with the file.删除了文件附带的 Login.cshtml。 NB.注意。 This is MVC.ASP.NET with Individual Account User这是带有个人帐户用户的 MVC.ASP.NET
  2. Each time the page gets loaded, it fetch the maping route and hit the controller.每次加载页面时,它都会获取映射路由并点击控制器。 Its working now.它现在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM