razorwebmatrixserver.mappath

WebMatrix - Server.MapPath issues


I am having an issue with the server.mappath method. My current code is:

var imageroot = Server.MapPath("~/Images/Property/");
var foldername =  rPropertyId.ToString();
var path = Path.Combine(imageroot, foldername);

When I upload this path into my database, I expect to see the following URL:

/images/property/1/filename.jpg

But what I actually see is this URL:

C:\Users\gavin\Dropbox\My Web Sites\StayInFlorida\Images\Property\1\filename.jpg

How do I get around this? I'm assuming I have to change the MapPath method, but I've tried a few things but I've had no luck.


Solution

  • The Server.MapPath method returns a file system path. What you want is a (relative) URL. Paths and URLs are completely different things. Typically, you need a path if you want to manage files in your server side code, and you need a URL is you are providing access to those files to visitors via hyperlinks. URLs can be constructed from strings:

    var url = string.Format("/Images/Property/{0}/{1}", rProprtyId, filename)