Wednesday, January 4, 2012

MapPath ASP.NET

MapPath sometimes refers to the directory the .NET solution is in and not the website and sometimes it does refers to the website itself. If we add the tilde character (~) it will return the website's path.

For example I have my solution at C:\Projects\src\MeinApp and inside that directory is the website (C:\Projects\src\MeinApp\WebService).
MapPath("/Schemas") will return 'C:\Projects\src\MeinApp\Schemas\' but
MapPath("~/Schemas") will return 'C:\Projects\src\MeinApp\WebService\Schemas'
which is what we finally want.

string dir = HostingEnvironment.MapPath("~/Schemas");

No comments: