Allow relative ("~") paths without linking to current page?

I am writing a shared library for use by all of my web projects. One of the features is constant call forwarding, as shown below:

public static void PermanentRedirect(string url, HttpResponse response, bool endResponse)
{
    url = //resolve url here. how?

    response.Status = "301 Moved Permanently";
    response.AddHeader("Location", url);

    if (endResponse) response.End();
}

How can I resolve the URL without submitting the current page to access Page.ResolveUrl? Note that I can change the method signature, but I would prefer not, because I think this will overload the API.

I have a couple of overloads for this, so my desired use is:

WebUtility.PermanentRedirect("~/somewhere/somepage.aspx")
+3
source share
3 answers

I believe you can use HttpRuntime.AppDomainAppVirtualPath as a direct tilde replacement.

VirtualPathUtility.ToAbsolute, - (, , UriBuilder).

- , IMHO HttpResponse .

+4

:

url = VirtualPathUtiliy.ToAbsolute(url);
+2

You can get the current page instance with HttpContext.Handler:

var page = HttpContext.Current.Handler as Page;
if (page != null)
{
     // Use page instance, f.e. page.Request.Url;
}

By the way, you do not need to pass HttpResponse:

HttpResponse response = HttpContext.Current.Response;

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.response.aspx

+1
source

All Articles