Server.MapPath(quot;.quot;), Server.MapPath(quot;~quot;), Server.MapPath(@quot;quot;), Server.MapPath(quot;/quot;). What is the difference?(Server.MapPath(.)、Server.MapPath(~)、Server.MapPath(@)、Server.MapPath(/).有什么区别?)
问题描述
谁能解释一下Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"") 和 Server.MapPath("/")?
Can anyone explain the difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"") and Server.MapPath("/")?
推荐答案
Server.MapPath 指定映射到物理目录的相对或虚拟路径.
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".")1 返回正在执行的文件(例如aspx)的当前物理目录Server.MapPath("..")返回父目录Server.MapPath("~")返回应用程序根目录的物理路径Server.MapPath("/")返回域名根的物理路径(不一定和应用的根相同)
Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executedServer.MapPath("..")returns the parent directoryServer.MapPath("~")returns the physical path to the root of the applicationServer.MapPath("/")returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
一个例子:
假设您将网站应用程序 (http://www.example.com/) 指向
Let's say you pointed a web site application (http://www.example.com/) to
C:Inetpubwwwroot
并在
D:WebAppsshop
例如,如果您在以下请求中调用 Server.MapPath():
For example, if you call Server.MapPath() in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
然后:
Server.MapPath(".")1 返回D:WebAppsshopproductsServer.MapPath("..")返回D:WebAppsshopServer.MapPath("~")返回D:WebAppsshopServer.MapPath("/")返回C:InetpubwwwrootServer.MapPath("/shop")返回D:WebAppsshop
Server.MapPath(".")1 returnsD:WebAppsshopproductsServer.MapPath("..")returnsD:WebAppsshopServer.MapPath("~")returnsD:WebAppsshopServer.MapPath("/")returnsC:InetpubwwwrootServer.MapPath("/shop")returnsD:WebAppsshop
如果 Path 以正斜杠 (/) 或反斜杠 () 开头,则 MapPath() 返回的路径为如果 Path 是完整的虚拟路径.
If Path starts with either a forward slash (/) or backward slash (), the MapPath() returns a path as if Path was a full, virtual path.
如果 Path 不以斜杠开头,则 MapPath() 返回相对于正在处理的请求的目录的路径.
If Path doesn't start with a slash, the MapPath() returns a path relative to the directory of the request being processed.
注意:在 C# 中,@ 是逐字字面字符串运算符,表示字符串应该按原样"使用,而不是针对转义序列进行处理.
Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.
脚注
Server.MapPath(null)和Server.MapPath("")将 也产生这种效果.
Server.MapPath(null)andServer.MapPath("")will produce this effect too.
这篇关于Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"")、Server.MapPath("/").有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"")、Server.MapPath("/").有什么区别?
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
