简体   繁体   English

如何在ASP.NET MVC中解析对文件的文件路径引用

[英]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. 嗨,如何在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. 假设我在content / image / img.jpg中有一个图像文件。”如何在asp.net mvc中获取文件的物理路径。

You could use the MapPath method: 您可以使用MapPath方法:

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: 如果您在视图内,则不需要物理路径,而需要考虑虚拟目录的相对路径,因此您应该使用Url帮助器:

<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. 而且,如果您在应用程序的其他某些层中,而该其他层没有对HttpContext的直接访问权限,则您不应该获取文件的任何物理路径,则应将该路径注入/传递给相应的层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM