惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - deerchao

Unity 在平面上播放含透明通道的视频(Play video with alpha channel on plane in unity) 域名迁移 错误721 -- 在虚拟机中连接VPN, 显示验证用户名和密码之后出错 - deerchao 繁体编码文本文件转换为简体编码的工具 生成VB多行字符串常量的工具 64位虚拟机Guest OS安装错误:0xC0000225 在64bit Win2008上运行Asp + Access网站 工具: 删除Visual Studio项目中文件链接,并把原文件复制到相应的目录 一个代表年月的类YearMonth Tip: Resharper 中 "Unknown Comment" 问题的解决办法 Struct与赋值 Tips 测试ConnectionString是否能连接上数据库服务器 奇怪的TreeView(WinForms)自动选中问题 From C# to VB A fast object clone class - using Expression.Compile() jQuery.combobox, 给文本框添加下拉选项的轻量级插件 CDTray, 打开,关闭光驱的系统托盘程序 jQuery.Excel, 使用Ctrl+方向键/Home/End在input表格中移动
MapPath的反函数(Reversing MapPath)
deerchao · 2009-10-26 · via 博客园 - deerchao

首先需要说明的一点是,目前Google到的结果,前几个解决方案是错的,因为它没有考虑到应用的虚拟根目录不是/的情况.

比如,如果应用的物理根目录是c:\website\piic\en,应用的虚拟路径为/en/,文件的路径为c:\websites\piic\en\images\aaa.jpg的话,按那几个解决方案,得到的结果将会是/images/aaa.jpg,而实际上应该是/en/images/aaa.jpg.

并且,MapPath是可以这么用的: MapPath("~/aaa.jpg"), 上述解决方案也没有处理这个特殊的目录(~).

下面附上我的解决方案:

Code
        /// <summary>
        
/// Reverse of MapPath
        
/// </summary>
        
/// <param name="path">File path in the disk</param>
        
/// <returns>virtual path in current web application</returns>
        public static string PathMap(string path)
        {
            
// example 1:
            
// path: c:\websites\piic\en\images\aaa.jpg
            
// application root: c:\website\piic\en
            
// virtual root: /
            
// result: /en/images/aaa.jpg// example 2:
            
// path: c:\websites\piic\en\images\aaa.jpg
            
// application root: c:\website\piic\en
            
// virtual root: /en/
            
// result: /en/images/aaa.jpg
if (path.StartsWith("~/"))
            {
                var virtualRoot 
= HostingEnvironment.ApplicationVirtualPath;
                path 
= virtualRoot + path.Substring(2);
            }

            var approot 

= HostingEnvironment.ApplicationPhysicalPath;return path.Replace(approot, string.Empty).Replace('\\''/');
        }