We help IT Professionals succeed at work.

How to make a path relative?

mousemat24
mousemat24 asked
on
Hi there

Really need some help:

I have this URL

private const string DummyPageUrl = "~/Service.aspx";


I also have a method called

private void HitPage()
{
            WebClient client = new WebClient();
            var url = HttpRuntime.AppDomainAppVirtualPath + DummyPageUrl;  // this is wrong?
            client.DownloadData(url);
}

How do I make the path relative i.e

http://test.com/books/Service.aspx

Thanks
Comment
Watch Question

Kiran SonawaneProject Lead
Top Expert 2011

Commented:
Try like this

private void HitPage()
{
            WebClient client = new WebClient();
            var url = HttpRuntime.AppDomainAppVirtualPath + "/Service.aspx";
            client.DownloadData(url);

}

Author

Commented:
just done that, I get an error:

Could not find file 'C:\RunningAsService.aspx'.

private const string DummyPageUrl = "RunningAsService.aspx";
.
.
.
WebClient client = new WebClient();
var url = HttpRuntime.AppDomainAppVirtualPath + DummyPageUrl;
client.DownloadData(url);

Author

Commented:
if I put private const string DummyPageUrl = "/RunningAsService.aspx";


I get this error:

The UNC path should be of the form \\server\share.

Author

Commented:
if I put private const string DummyPageUrl = "~/RunningAsService.aspx";


I get this error:

Could not find a part of the path 'C:\~\RunningAsService.aspx'.

Author

Commented:
note: this is in the global.asax file
Project Lead
Top Expert 2011
Commented:
You need complete virtual path not "relative" path to run service.aspx page

Here is the solution to work around it http://stackoverflow.com/questions/932745/how-do-i-get-the-complete-virtual-path-of-an-asp-net-application
Rahul GadeSr. Architect

Commented:
If you are running this on client side, then you won't be able to get the server path, the virtual directory mapping is suppose to hide it as a part of security. You are suppose to know the complete url if you are accessing this from client side.

On server side, you can use :

var url = HttpContext.Current.Server.MapPath("~/Service.aspx");


-Rahul Gade