2

hi how to resolve file path reference to a file in asp.net mvc. Suppose I have an image file in the content/image/img.jpg". How do i get the physical path of the file in asp.net mvc.

1
  • ~/content/image/img.jpg should work Commented Apr 2, 2011 at 9:11

1 Answer 1

7

You could use the MapPath method:

public ActionResult Index()
{
    string physicalPath = Server.MapPath("~/content/image/img.jpg");
    ...
}

and if you are inside a view you don't need the physical path but a relative path taking into account the virtual directory so you should be using Url helpers:

<img src="<%= Url.Content("~/content/image/img.jpg") %>" alt="" />

And if you are inside some other layer of the application which doesn't have direct access to the HttpContext you shouldn't be getting any physical path to a file, this path should be injected/passed to the corresponding layer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.