Thursday, November 8, 2012

Get absolute server path of resources in ASP.NET MVC

In a scenario where you want to load images or other resources, Url.Content() does not return the exact resource path like this (D:\Cart\CartProject\Images\Item4.jpg). Hence, resulting into a null exception or empty resource.

Instead of using Url.Content() or ResolveUrl(), i get the server path of the resource using HttpContext of the application.
return new ImageResult(HttpContext.Server.MapPath("~/Images/Item" + id + ".jpg")); 
Note: Testing is done locally and not on a virtual directory...

Source: Find Absolute Path the the App Data Folder from Controller Cheers!


Post a Comment