Javascript Asp.net MVC 5视图在使用F5重新加载页面之前不呈现

Javascript Asp.net MVC 5视图在使用F5重新加载页面之前不呈现,javascript,c#,jquery,asp.net-mvc-5,signalr,Javascript,C#,Jquery,Asp.net Mvc 5,Signalr,当我的应用程序的用户直接按下“注销”按钮时,他们将注销并返回到登录页面(两个操作见下面的代码)。这是工作的预期,但管理员用户有能力强制注销个人用户。我目前正在用信号器完成这项工作,遇到了一个奇怪的问题 当我从客户机方法重定向到注销时,一切都被正确调用。客户端用户已注销,并且重定向到操作会正确重定向到登录操作。但是,在我用F5手动刷新页面之前,LogIn视图不会呈现。我完全无法理解为什么视图没有第一次渲染 注销操作 public ActionResult LogOff() {

当我的应用程序的用户直接按下“注销”按钮时,他们将注销并返回到登录页面(两个操作见下面的代码)。这是工作的预期,但管理员用户有能力强制注销个人用户。我目前正在用信号器完成这项工作,遇到了一个奇怪的问题

当我从客户机方法重定向到注销时,一切都被正确调用。客户端用户已注销,并且
重定向到操作
会正确重定向到
登录
操作。但是,在我用F5手动刷新页面之前,
LogIn
视图不会呈现。我完全无法理解为什么视图没有第一次渲染

注销操作

    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();

        return RedirectToAction("Login", "Account");
    }
    public ActionResult Login(string returnUrl, string displayMsg)
    {
        ViewBag.ReturnUrl = returnUrl;
        ViewBag.DisplayMsg = displayMsg;
        ViewBag.PasswordAttempts = 0;
        return View();
    }
登录操作

    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();

        return RedirectToAction("Login", "Account");
    }
    public ActionResult Login(string returnUrl, string displayMsg)
    {
        ViewBag.ReturnUrl = returnUrl;
        ViewBag.DisplayMsg = displayMsg;
        ViewBag.PasswordAttempts = 0;
        return View();
    }
信号器客户端方法

$.connection.hub.start();
authHub.client.LogOut = function () {
    $.ajax({
        url: '@Url.Action("LogOff", "Account", new { area = ""})',
        type: "POST"
    });
    $.connection.hub.stop();
}
登录视图

section id="loginForm">

            <div class="col-md-12">
                <div class="wrap">
                    <p class="form-title">Log In</p>

                        @using (Html.BeginForm("Login","Account",new {ViewBag.ReturnUrl, ViewBag.PasswordAttempts}, FormMethod.Post, new{@class="login"}))
                        {
                            @Html.AntiForgeryToken()
                            <section class="DisplayMsg">@ViewBag.DisplayMsg</section>

                            <section>@Html.ValidationMessageFor(m => m.UserName) </section>
                            <label>User Name:</label> @Html.TextBoxFor(m => m.UserName)

                            <section>@Html.ValidationMessageFor(m => m.Password)</section>
                            <label>Password:</label>@Html.PasswordFor(m => m.Password)

                            <input type="submit" value="Log In" class="btn btn-success btn-sm" />
                                @*<div class="reset-forgot">
                                    <div class="row">
                                        <div class="col-md-6 reset-pass-content">
                                            @Html.ActionLink("Reset Password", "PasswordReset", "Account", new {area = string.Empty})
                                        </div>
                                    </div>
                                </div>*@
                        }
                </div>
            </div>
section id=“loginForm”>

登录

@使用(Html.BeginForm(“Login”,“Account”,new{ViewBag.ReturnUrl,ViewBag.passwordtruments},FormMethod.Post,new{@class=“Login”})) { @Html.AntiForgeryToken() @ViewBag.DisplayMsg @Html.ValidationMessageFor(m=>m.UserName) 用户名:@Html.TextBoxFor(m=>m.UserName) @Html.ValidationMessageFor(m=>m.Password) 密码:@Html.PasswordFor(m=>m.Password) @* @ActionLink(“重置密码”、“密码重置”、“帐户”,新{area=string.Empty}) *@ }

因为您执行了一个Ajax调用,它永远不会将用户重定向到登录页面,它只会在服务器端注销用户。在服务器中注销后,您可以使用
location.href
将用户重定向到登录页面:

$.connection.hub.start();
   authHub.client.LogOut = function () {
      $.connection.hub.stop();
      $.ajax({
      url: '@Url.Action("LogOff", "Account", new { area = ""})',
      type: "POST",
      success: function(){
          location.href = '@Url.Action("Login", "Account")'
      }
  });
}