1

如果登录成功,我的登录控制器中有一个 javascript 重定向,它会打开我的主页: return JavaScript("window.top.location ='" + returnUrl + "';")

我的登录控制器被 Ajax.BeginForm 内的 sumbit 作为局部视图调用。

在开发(我的 vs2010 上的 F5)时,我的解决方案工作完美,但我发布的版本(我上传到服务器的版本)没有重新渲染一个全新的视图,而是刷新了我的 targetid 中的 div。

我的部分查看代码

@model Web.Models.LogOnModel
<div id="LogBox2">
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "LogBox2",  }))
{         
<fieldset>...'All textbox capturing data here

            <input type="submit" value="LogOn" />           
</fieldset>
@Html.ValidationSummary(true, "Login was unsuccessful")
}
 </div>

我的控制器代码

 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                      && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return JavaScript("location.href ='" + returnUrl + "';");
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            if (Request.IsAjaxRequest())
            {
                // If an ajax request was made return only the validation errors 
                // instead of the whole page
                return PartialView("LogIn2"); 
            }
            else
            {
                return View();
            }
        }

我真的不明白为什么调试(F5)可以正常工作但未发布。你?提前感谢

4

1 回答 1

2

不确定我是否完全理解您的情况。我认为您的作品正在按应有的方式进行;考虑以下场景:表单正在通过 Ajax 提交,并且“returnUrl”为空/无效,然后 div“LogBox2”将使用“Home/Index”行的响应进行更新return RedirectToAction("Index", "Home");

如果您的意图是在登录成功时将用户重定向到路径“ returnUrl ”或“ home/index ”,那么一种解决方案可能是替换该行

return RedirectToAction("Index", "Home");

return JavaScript("location.href =" + HttpUtility.JavaScriptStringEncode(Url.Action("Index", "Home"), true) + ";");

您还可以考虑将jQuery Ajax 与 ASP.NET MVC 一起使用。

于 2012-04-13T05:48:57.773 回答