从部分视图重定向 - 已发布不起作用,但开发是可以的

Redirect From Partial View - PUBLISHED not working but DEVELOPING is OK

本文关键字:开发 不起作用 视图 重定向      更新时间:2023-09-26

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

我的登录控制器由 Ajax.BeginForm 中的 sumbit 作为部分视图调用。

在开发(我的 vs5 上的 F2010)时,我的解决方案工作得很好,但我发布的版本(我上传到服务器的版本)并没有重新生成一个完整的新视图,而是刷新了我的目标 id 中的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) 工作正常 bur 已发布不。 你呢?提前感谢

不确定我是否完全理解您的情况。我认为您的生产正在正常工作;考虑以下场景:表单是通过 Ajax 提交的,并且 'returnUrl' 为 null/无效,然后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一起使用。