Question

I am running a timer and performing a redirect after a user changes his or her password (AKA, notify them that the password was changed and then send them back to the home page). However, I can't seem to do a relative path in the code below. Listed below is what I'd like to do:

Response.AddHeader("REFRESH", "2;URL="~/pages/home.aspx");

Why isn't this working? How do I get it to work? (I know I can do relative paths in other parts of the site, but that is because it is running server side.) Thanks.

Was it helpful?

Solution

You need to specify an absolute URL for the REFRESH header. Take a look at this post showing how to obtain an absolute URL from relative (you could use the ResolveServerUrl shown there):

Response.AddHeader("REFRESH", "2;url=" + ResolveServerUrl("~/pages/home.aspx"));

For reference:

/// <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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top