C# 如何修复登录asp.net mvc以加载页面?
我的登录页面每次都无法启动。错误是 “/”应用程序中出现服务器错误。找不到资源 我重新检查了以下文件RouteConfig.cs,并将路线映射为以下内容,包括带有操作方法的控制器。现在的问题是,如何才能不加载此页面?刚才已加载并检查了注册表页C# 如何修复登录asp.net mvc以加载页面?,c#,asp.net-mvc,C#,Asp.net Mvc,我的登录页面每次都无法启动。错误是 “/”应用程序中出现服务器错误。找不到资源 我重新检查了以下文件RouteConfig.cs,并将路线映射为以下内容,包括带有操作方法的控制器。现在的问题是,如何才能不加载此页面?刚才已加载并检查了注册表页 // Controller class // GET: /Account/Login [AllowAnonymous] public ActionResult Login(string returnUrl)
// 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>
}
//控制器类
//获取:/Account/登录
[异名]
公共操作结果登录(字符串返回URL)
{
TempData[“ErrorMessage”]=“”;
TempData[“LoginMessage”]=“”;
ViewBag.ReturnUrl=返回URL;
返回视图();
}
//
//POST:/帐户/登录
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务
登录以启动会话
@使用(Html.BeginForm(“Index”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{@role=“form”}))
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(m=>m.Email,新的{@class=“form control”,type=“Email”,placeholder=“Email”,autofocus=“autofocus”,required=“required”})
@Html.PasswordFor(m=>m.Password,新的{@class=“form control”,type=“Password”,placeholder=“Password”})
@CheckBoxFor(m=>m.RememberMe,新的{@type=“checkbox”,id=“memory”})
记得我吗
登录
}
@*@Html.Partial(“_externalLoginListPartial”,新的ExternalLoginListViewModel{ReturnUrl=ViewBag.ReturnUrl})*@
身份验证失败!
@TempData[“ErrorMessage”]
好啊
函数名为popup(){
$(“模态危险”).modal();
}
@if(ViewBag.JavaScriptFunction!=null)
{
@Html.Raw(ViewBag.JavaScriptFunction)
}
嗯,为什么不这样做呢
routes.MapRoute(
name: "Login",
url: "Account/Index",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
您的路由具有索引页,但未创建任何索引(HTTP GET)页。这就是为什么会出现这个错误
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 }
);
很抱歉,请尝试删除第一个/以及您不想使用默认路由的原因。MapRoute(“default”、“{controller}/{action}/”、new{controller=“Home”、action=“Index”、})@Makrs让我忙着做一些快速的事情,让你知道我是如何解决这个问题的。它是否影响了控制器方法
索引
?