开发者

how to resolve file path reference to a file in asp.net mvc

hi how to resolve file path reference to a file in asp.net mvc. Suppose I have an image file in the content/image/i开发者_开发百科mg.jpg". How do i get the physical path of the file in asp.net mvc.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜