我正在运行计时器并在用户更改其密码后执行重定向(AKA,通知他们密码已更改,然后将其发送回主页)。但是,我似乎无法在下面的代码中执行相对路径。下面列出了我喜欢做的事情:
Response.AddHeader("REFRESH", "2;URL="~/pages/home.aspx");
为什么这不起作用?我如何让它工作? (我知道我可以在网站的其他部分做相对路径,但这是因为它正在运行服务器端。)谢谢。
答案 0 :(得分:1)
您需要为REFRESH标头指定绝对URL。看一下this post,了解如何从亲戚那里获取绝对网址(你可以使用那里显示的ResolveServerUrl
):
Response.AddHeader("REFRESH", "2;url=" + ResolveServerUrl("~/pages/home.aspx"));
供参考:
/// <summary>
/// This method returns a fully qualified absolute server Url which includes
/// the protocol, server, port in addition to the server relative Url.
///
/// Works like Control.ResolveUrl including support for ~ syntax
/// but returns an absolute URL.
/// </summary>
/// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
/// <param name="forceHttps">if true forces the url to use https</param>
/// <returns></returns>
public static string ResolveServerUrl(string serverUrl, bool forceHttps)
{
// *** Is it already an absolute Url?
if (serverUrl.IndexOf("://") > -1)
return serverUrl;
// *** Start by fixing up the Url an Application relative Url
string newUrl = ResolveUrl(serverUrl);
Uri originalUri = HttpContext.Current.Request.Url;
newUrl = (forceHttps ? "https" : originalUri.Scheme) +
"://" + originalUri.Authority + newUrl;
return newUrl;
}
/// <summary>
/// This method returns a fully qualified absolute server Url which includes
/// the protocol, server, port in addition to the server relative Url.
///
/// It work like Page.ResolveUrl, but adds these to the beginning.
/// This method is useful for generating Urls for AJAX methods
/// </summary>
/// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
/// <returns></returns>
public static string ResolveServerUrl(string serverUrl)
{
return ResolveServerUrl(serverUrl, false);
}