【问题标题】:How can I redirect MVC5 RedirectResult () to a full page in Asp.net?如何将 MVC5 RedirectResult () 重定向到 Asp.net 中的整页?
【发布时间】:2020-01-19 13:05:53
【问题描述】:

在我的 ASP.NET MVC5 页面中,我有一个带有超时的个人登录功能。这很好用,一旦超时过期,登录页面会在下一个操作中打开,然后继续。 只有一个布局问题。如果我在 PartialView 中单击已注销的用户,登录页面将在此 PartialView 中呈现,而不是在整个页面上。 验证码:

        public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
    {
        if (filterContext.Result == null || filterContext.Result is HttpUnauthorizedResult)
        {
            filterContext.HttpContext.Session["PlantId"] = filterContext.HttpContext.Request.QueryString["PlantId"];
            filterContext.HttpContext.Session["ShowOnlyEnabledRecipes"] = filterContext.HttpContext.Request.QueryString["ShowOnlyEnabledRecipes"];
             filterContext.Result = new RedirectResult("~/UserLogin/Login");
        }
    }

和控制器:

    [HttpPost]
    public ActionResult Login(LoginModel loginModel)
    {
        Initialize();
         ...
        return View(loginModel);
            }

如何确保登录表单始终占据整个页面?另外,如果它是通过单击 PartialView 来调用的?

【问题讨论】:

    标签: asp.net authentication asp.net-mvc-5


    【解决方案1】:

    在 PartialView(PartialView 的控制器)中进行重定向,结果只呈现在该区域,如果要定向到另一个页面(更改 url),您需要在控制器中添加以下代码。您将获得来自 Ref 链接的高级信息。

    附: ActionFilter 无法返回 JavaScript,请放入普通控制器。

    参考:http://geekswithblogs.net/DougLampe/archive/2011/08/05/redirecting-from-a-partial-view-the-right-way-in-asp.net.aspx

    string virtualPath = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;
    string returnUrl = virtualPath + "/UserLogin/Login";
    return JavaScript( string .Format( "document.location = '{0}';" , returnUrl));
    

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2016-04-23
    • 2021-02-24
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多