开发者

MVC3 uri with or without "/" show different response

开发者 https://www.devze.com 2023-03-11 21:17 出处:网络
What is diffe开发者_运维知识库rence between these two paths? http://www.mydomain.com/testmvc3 http://www.mydomain.com/testmvc3/

What is diffe开发者_运维知识库rence between these two paths?

http://www.mydomain.com/testmvc3

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

I put the code in HomeController:

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

But only the second link works fine, but the first one still show the Home page.(even it is authenticated) How to make them have the same react?


I found the problem, it was caused by the page cache. To avoid the problem, I modify the code to:

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

Now it works.


You will want to leave the trailing slash off of a normal route, otherwise it indicates that there may be url parameters coming into the action.

To enforce this you might want to check out the cleanurl filter that is in MvcCms. 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;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号