【问题标题】:MVC3 uri with or without "/" show different response带或不带“/”的 MVC3 uri 显示不同的响应
【发布时间】:2011-09-09 13:48:07
【问题描述】:

这两条路径有什么区别?

http://www.mydomain.com/testmvc3
http://www.mydomain.com/testmvc3/

我把代码放在 HomeController 里:

// GET: /Home/
public ActionResult Index()
{
    if (Request.IsAuthenticated)
    {
        return RedirectToAction("Index", "Member");
    }
    else
    {
        return View();
    }
}

但只有第二个链接可以正常工作,但第一个仍然显示主页。(即使它是经过身份验证的)如何使它们具有相同的反应?

【问题讨论】:

    标签: asp.net-mvc-3 razor uri redirecttoaction


    【解决方案1】:

    您将希望在正常路由中保留尾部斜杠,否则表明可能有 url 参数进入操作。

    要强制执行此操作,您可能需要查看 MvcCms 中的 cleanurl 过滤器。 Source Code

        private bool IsTrailingSlashDirty(ref string path)
        {
            //we only want a trailing slash on the homepage
            if (path.EndsWith("/") && !path.Equals("/"))
            {
                path = path.TrimEnd(new char[] { '/', '/' });
                return true;
            }
            return false;
        }
    

    【讨论】:

    • MvcCmsJon,感谢您的重播。您是对的,使用您的代码,网址显然会清晰。
    【解决方案2】:

    我找到了问题,是页面缓存引起的。为了避免这个问题,我将代码修改为:

    [OutputCache(Duration = 30, VaryByCustom = "Request.IsAuthenticated")]
    public ActionResult Index()
    {
        if (Request.IsAuthenticated)
        {
            return RedirectToAction("Index", "Member");
        }
        else
        {
            return View();
        }
    }
    

    现在可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多