如何解析相对于ASP的路径。NET MVC 4应用程序根?

时间:2022-01-21 04:12:19

How do I resolve paths relative to an ASP.NET MVC 4 application's root directory? That is, I want to open files belonging to the application from controller actions, referenced like ~/Data/data.html. These paths are typically specified in Web.config.

如何解析相对于ASP的路径。NET MVC 4应用程序的根目录?也就是说,我想从控制器动作中打开属于应用程序的文件,比如~/Data/ Data .html。这些路径通常在Web.config中指定。

EDIT:

编辑:

By 'resolve' I mean to transform a path relative to the application's root directory to an absolute path, .e.g. ~/Data/data.htmlC:\App\Data\Data.html.

我所说的“解析”是指将相对于应用程序根目录的路径转换为绝对路径。~ /数据/数据。html→C:\ App \ Data \研究司。

4 个解决方案

#1


72  

To get the absolute path use this:

要获得绝对路径,请使用以下方法:

String path = HttpContext.Current.Server.MapPath("~/Data/data.html");

EDIT:

编辑:

To get the Controller's Context remove .Current from the above line. By using HttpContext by itself it's easier to Test because it's based on the Controller's Context therefore more localized.

要获取控制器的上下文,请从上面一行中删除. current。通过使用HttpContext本身,测试更容易,因为它基于控制器的上下文,因此更本地化。

I realize now that I dislike how Server.MapPath works (internally eventually calls HostingEnvironment.MapPath) So I now recommend to always use HostingEnvironment.MapPath because its static and not dependent on the context unless of course you want that...

我现在意识到我不喜欢服务器。MapPath可以工作(内部最终调用HostingEnvironment.MapPath),所以我现在建议始终使用HostingEnvironment。MapPath因为它是静态的,不依赖于上下文,除非你想要。

#2


49  

I find this code useful when I need a path outside of a controller, such as when I'm initializing components in Global.asax.cs:

当我需要控制器之外的路径时,比如在Global.asax.cs中初始化组件时,我发现这些代码很有用:

HostingEnvironment.MapPath("~/Data/data.html")

#3


10  

Just use the following

用下面的

Server.MapPath("~/Data/data.html")

#4


4  

In the action you can call:

在行动中你可以调用:

this.Request.PhysicalPath

that returns the physical path in reference to the current controller. If you only need the root path call:

这将返回引用当前控制器的物理路径。如果您只需要根路径调用:

this.Request.PhysicalApplicationPath

#1


72  

To get the absolute path use this:

要获得绝对路径,请使用以下方法:

String path = HttpContext.Current.Server.MapPath("~/Data/data.html");

EDIT:

编辑:

To get the Controller's Context remove .Current from the above line. By using HttpContext by itself it's easier to Test because it's based on the Controller's Context therefore more localized.

要获取控制器的上下文,请从上面一行中删除. current。通过使用HttpContext本身,测试更容易,因为它基于控制器的上下文,因此更本地化。

I realize now that I dislike how Server.MapPath works (internally eventually calls HostingEnvironment.MapPath) So I now recommend to always use HostingEnvironment.MapPath because its static and not dependent on the context unless of course you want that...

我现在意识到我不喜欢服务器。MapPath可以工作(内部最终调用HostingEnvironment.MapPath),所以我现在建议始终使用HostingEnvironment。MapPath因为它是静态的,不依赖于上下文,除非你想要。

#2


49  

I find this code useful when I need a path outside of a controller, such as when I'm initializing components in Global.asax.cs:

当我需要控制器之外的路径时,比如在Global.asax.cs中初始化组件时,我发现这些代码很有用:

HostingEnvironment.MapPath("~/Data/data.html")

#3


10  

Just use the following

用下面的

Server.MapPath("~/Data/data.html")

#4


4  

In the action you can call:

在行动中你可以调用:

this.Request.PhysicalPath

that returns the physical path in reference to the current controller. If you only need the root path call:

这将返回引用当前控制器的物理路径。如果您只需要根路径调用:

this.Request.PhysicalApplicationPath