Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

How to make a path relative?

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
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Try like this

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

}
Avatar of mousemat24
mousemat24

ASKER

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);
if I put private const string DummyPageUrl = "/RunningAsService.aspx";


I get this error:

The UNC path should be of the form \\server\share.
if I put private const string DummyPageUrl = "~/RunningAsService.aspx";


I get this error:

Could not find a part of the path 'C:\~\RunningAsService.aspx'.
note: this is in the global.asax file
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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