相对 URL 的绝对文件路径
我看过很多关于将相对 url 解析为绝对路径的教程,但我想做相反的事情:将系统绝对文件路径解析为相对 url。
有没有一种很好的无黑客方法来转换像 c:\my_website_root\images\picture_a.jpg
这样的文件路径 进入 images/picture_a.jpg
我已经查看了 Uri.MakeRelative()
但我认为它在这种情况下没有用处。
编辑:我已经像这样实现了它,但看起来仍然很hacky(特别是第2行)
var urlPath = new Uri(@"c:\mywebfolder\images\picture1.jpg");
var urlRoot = new Uri(Server.MapPath("~")+"/");
string relative = urlRoot.MakeRelativeUri(urlPath).ToString();
I've seen lots of tutorials on resolving a relative url to an absolute path, but i want to do the opposite: resolve an system absolute filepath into a relative url.
Is there a nice hack-free way to turn a filepath like c:\my_website_root\images\picture_a.jpg
into images/picture_a.jpg
I've had a look at Uri.MakeRelative()
but i dont think it will be of use in this case.
Edit: I've implemented it like this, still seems hacky (esp line#2)
var urlPath = new Uri(@"c:\mywebfolder\images\picture1.jpg");
var urlRoot = new Uri(Server.MapPath("~")+"/");
string relative = urlRoot.MakeRelativeUri(urlPath).ToString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 IIS 中,设置虚拟目录
images
并将其指向c:\my_website_root\images\
。如果您的网站已定向到
c:\my_website_root\
,则您无需执行任何操作。In IIS, setup a virtual directory
images
and point it toc:\my_website_root\images\
.If your website is already directing to
c:\my_website_root\
, you don't need to do anything.如果您需要将所有相对 url 转换为绝对 url,请使用此函数:
If you need to convert all the relative urls to absolute urls use this fucntion: