【问题标题】:Forcing the user to go through another page before getting to the one in the URL强制用户在访问 URL 中的页面之前浏览另一个页面
【发布时间】:2013-10-31 15:02:57
【问题描述】:

例如,如果用户尝试访问Main 操作并在地址栏中输入mySite/Main,我希望将其强制路由到mySite/beforeMain,然后在一段时间后自动重定向到Main加工。我如何使用路由映射来做到这一点,或者这是否可以通过其他方式实现?我不想在 URL 中有查询字符串参数,或者使用 TempData/etc。以防 cookie 被禁用。

【问题讨论】:

    标签: c# asp.net-mvc-4 routes


    【解决方案1】:

    也许你可以使用Url Rewrite

    您也可以使用RedirectToAction:

    public ActionResult Main()
    {
        if(Request.QueryString["redirected"].ToString() != "true")
        {
            return RedirectToAction("beforeMain", "mySite");
        }
    
        return View();
    }
    

    那么您的beforeMain 操作可能是:

    public ActionResult BeforeMain()
    {
         //do some stuff
         return RedirectToAction("Main",  new { redirected = "true" });
    }
    

    然后当你第二次点击 main 时,它会跳过重定向,你不会陷入循环。

    【讨论】:

    • 谢谢,我会检查 Url Rewrite,但除此之外:从 main 重定向的问题是我不知道这是否是我第一次重定向,因为beforeMain 会重定向回来。没有检查,就会有一个重定向循环——但是没有路由值或 TempData,我无法检查我能想到的任何东西。
    • 是否可以不将查询字符串附加到 URL 中?这是我遇到的问题的一部分——由于附加的查询字符串,不想使用路由值。
    • 我猜你可以使用会话来存储一个值
    • HTTP 是无状态的,您需要某种方式在回发之间保存数据... 为什么要隐藏查询字符串?有UrlRewriting.net,您可以将“干净”的 url(没有查询字符串)重写为具有查询字符串的 URL。用户不会在他们的地址栏中看到它。不知道这如何与 MVC 一起工作,虽然我很害怕
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    相关资源
    最近更新 更多