C# 如何解析相对于 ASP.NET MVC 4 应用程序根的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12239006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I resolve a path relative to an ASP.NET MVC 4 application root?
提问by aknuds1
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.html→ C:\App\Data\Data.html.
“解析”是指将相对于应用程序根目录的路径转换为绝对路径,例如~/Data/data.html→ C:\App\Data\Data.html。
采纳答案by Nate-Wilkins
To get the absolute path use this:
要获取绝对路径,请使用以下命令:
String path = HttpContext.Current.Server.MapPath("~/Data/data.html");
EDIT:
编辑:
To get the Controller's Context remove .Currentfrom the above line. By using HttpContextby 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.MapPathworks (internally eventually calls HostingEnvironment.MapPath) So I now recommend to always use HostingEnvironment.MapPathbecause its static and not dependent on the context unless of course you want that...
我现在意识到我不喜欢Server.MapPath工作方式(内部最终调用HostingEnvironment.MapPath)所以我现在建议始终使用HostingEnvironment.MapPath它,因为它是静态的而不依赖于上下文,除非你当然想要......
回答by developer10214
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
回答by Otto Kanellis
Just use the following
只需使用以下
Server.MapPath("~/Data/data.html")
回答by Michael L Perry
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")
回答by Owen Pauling
In your controller use:
在您的控制器中使用:
var path = HttpContext.Server.MapPath("~/Data/data.html");
This allows you to test the controller with Moq like so:
这允许您使用 Moq 测试控制器,如下所示:
var queryString = new NameValueCollection();
var mockRequest = new Mock<HttpRequestBase>();
mockRequest.Setup(r => r.QueryString).Returns(queryString);
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Setup(c => c.Request).Returns(mockRequest.Object);
var server = new Mock<HttpServerUtilityBase>();
server.Setup(m => m.MapPath("~/Data/data.html")).Returns("path/to/test/data");
mockHttpContext.Setup(m => m.Server).Returns(server.Object);
var mockControllerContext = new Mock<ControllerContext>();
mockControllerContext.Setup(c => c.HttpContext).Returns(mockHttpContext.Object);
var controller = new MyTestController();
controller.ControllerContext = mockControllerContext.Object;

