Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapPath vs VirtualPathUtility.ToAbsolute

Tags:

c#

asp.net

I am using c#, asp.net and working on a web application.

I initially had a relative path as such which I needed to be an absolute path. The below works but need to get the absolute path:

    return Chart.RenderChartHTML("../../Charts/MSLine.swf");

I tried the following which didn't work (note that it gives me the complete path on my hard drive to .swf):

    string mslinepath = HttpContext.Current.Server.MapPath("Charts/MSLine.swf");

    return Chart.RenderChartHTML(mslinepath);

I then tried the following which works:

    string mslinepath = VirtualPathUtility.ToAbsolute("~/Charts/MSLine.swf");

    return Chart.RenderChartHTML(mslinepath);

Wondering why VirtualPathUtility.ToAbsolute works while the other one doesn't.

like image 617
Nate Pet Avatar asked Nov 07 '12 18:11

Nate Pet


1 Answers

MapPath returns the physical file path on your server which corresponds to the specified virtual path.
(Eg: "C:\inetpub\wwwroot\Charts\MSLine.swf")

ToAbsolute converts an app-relative virtual path (one starting with "~/") to an absolute virtual path.
(Eg: "/AppName/Charts/MSLine.swf")

like image 132
Richard Deeming Avatar answered Oct 13 '22 21:10

Richard Deeming