6

I use asp.net 4 and c#.

I have this code that allow me to find the physical path for an image. As you can see I get my machine physical pagh file:///C:.

string pathRaw = HttpContext.Current.Request.PhysicalApplicationPath + "Statics\\Cms\\Front-End\\Images\\Raw\\";

Result:

file:///C:/......../f005aba1-e286-4d9e-b9db-e6239f636e63.jpg

But I need display this image at the Front end of my web application so I would need a result like this:

http://localhost:1108/Statics/Cms/Front-End/Images/Raw/f005aba1-e286-4d9e-b9db-e6239f636e63.jpg

How to do it?

PS: I need to convert the result of variable pathRaw.

Hope I was able to express myself unfortunately I'm not sure about terminology in this case.

Thanks for your help!

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • is there a reason why you cannot just use the relative path (/Statistics/Cms/....) ? – Dave Long Jun 08 '11 at 09:55
  • thanks Dave for your comment but I need convert the result of pathRaw, I cannot use the relative path implicitly in my code. Do you have any idea how to do it? thanks in adnvace – GibboK Jun 08 '11 at 10:02
  • you cannot convert a physical path to a URL dynamically - only thing that would make any sense is if you knew the possible root values, and which URL they ultimately mapped to, and then did some string manipulation to replace the root path portion with the URL, and then back slashes with forward slashes... but I am still not understanding why you need to use the Physical Path to get to your URL ? Surely, if this is running inside an ASP.NET application, then you can use the relative path to serve up the image ? – Dave Long Jun 08 '11 at 10:17
  • Thanks Dave for your comment! – GibboK Jun 08 '11 at 12:39

5 Answers5

13

The easiest thing to do is get rid of the physical application path. If you cannot do that in your code, just strip it off the pathRaw variable. Like this:

public string GetVirtualPath( string physicalPath )
{
   if ( !physicalPath.StartsWith( HttpContext.Current.Request.PhysicalApplicationPath ) )
   {
      throw new InvalidOperationException( "Physical path is not within the application root" );
   }

   return "~/" + physicalPath.Substring( HttpContext.Current.Request.PhysicalApplicationPath.Length )
         .Replace( "\\", "/" );
}

The code first checks to see if the path is within the application root. If not, there's no way to figure out a url for the file so an exception is thrown.

The virtual path is constructed by stripping off the physical application path, convert all back-slashes to slashes and prefixing the path with "~/" to indicate it should be interpreted as relative to the application root.

After that you can convert the virtual path to a relative path for output to a browser using ResolveClientUrl(virtualPath).

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
1

Get the root of your application using Request.ApplicationPath then use the answer from this question to get a relative path.

It might need a bit of tweaking but it should allow you to do what you're after.

Community
  • 1
  • 1
George Duckett
  • 31,770
  • 9
  • 95
  • 162
1

Left-strip your pathRaw content by the Request.ApplicationPath and construct the url using

Uri navigateUri = new Uri(System.Web.HttpContext.Current.Request.Url, relativeDocumentPath);
eFloh
  • 2,098
  • 20
  • 24
0

Make use of ApplicationPath - Gets the ASP.NET application's virtual application root path on the server.

Label1.Text = Request.ApplicationPath;
Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif";
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Pranay I trid with your code (i need deal with the pathRaw variable) and this is the result: http://localhost:1108/C:/....../f005aba1-e286-4d9e-b9db-e6239f636e63.jpg do you have any idea how to solve it? thanks – GibboK Jun 08 '11 at 10:07
  • I suppose you forgot to set `` – eFloh Jun 08 '11 at 11:17
-1

You can use Server.MapPath for this.

   string pathRaw = System.Web.HttpContext.Current.Server.MapPath(“somefile.jpg“);
sajoshi
  • 2,733
  • 1
  • 18
  • 22