ASP.NET: how to get virtual file path from a common handler?

How can I resolve the virtual file path to a browser-friendly path from a common .ashx handler?

eg. I want to convert:

~/asp/ClockState.aspx

at

/NextAllowed/asp/ClockState.aspx

If I was WebForm Page, I could call ResolveUrl:

Page.ResolveUrl("~/asp/ClockState.aspx")

which permits:

/NextAllowed/asp/ClockState.aspx

But I am not a WebForm page, I am a general handler. You know that an object IHttpHandlerwith all types of injections:

public class ResetClock : IHttpHandler 
{
    public void ProcessRequest (HttpContext context) 
    {
        //[process stuff]

        //Redirect client
        context.Response.Redirect("~/asp/ClockState.aspx", true);
    }

    public bool IsReusable { get { return false; } }
}
+5
source share
1 answer

You can use the VirtualPathUtility class to do this. It contains various methods of working with paths. The one you need is ToAbsolute (), which converts the relative path to absolute.

var path = VirtualPathUtility.ToAbsolute("~/asp/ClockState.aspx");

Response.Redirect , :

Response.Redirect("~/asp/ClockState.aspx");

URL- Response.Redirect.

+7

All Articles