HttpContext.Current.Response.Redirect is trying to send users to the wrong place

匆匆过客 提交于 2021-01-28 23:06:26

问题


I've got a section of code that redirects users to a page when their session times out:

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    string szCookieHeader = HttpContext.Current.Request.Headers["Cookie"];
    if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
    {
        HttpContext.Current.Response.Redirect("SessionTimeout.aspx");
    }
}

The problem is that IIS can't find SessionTimeout.aspx because its looking in the wrong place:

The file '/site/site/site/site/site/site/Site/SessionTimeout.aspx' does not exist.

With more and more "/site/" levels being added as time goes on. I can suppress the problem by creating a tree of ".../site/site/..." and placing an SessionTimeout.aspx in each but this is obviously not a true solution.

Does anyone know what's going on?


回答1:


Have you tried to use ~ ?

HttpContext.Current.Response.Redirect("~/SessionTimeout.aspx");

that will be resolved at runtime with the full url of your site.




回答2:


Your redirect is relative to the current page. Add ~/ to the url to have it look at the root of the site.

Your redirect should read:

 HttpContext.Current.Response.Redirect("~/SessionTimeout.aspx");


来源:https://stackoverflow.com/questions/5980037/httpcontext-current-response-redirect-is-trying-to-send-users-to-the-wrong-place

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!