开发者

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

开发者 https://www.devze.com 2023-02-21 19:35 出处:网络
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.Yo

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

精彩评论

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

关注公众号