相对 URL 的绝对文件路径

发布于 2024-08-19 11:47:32 字数 492 浏览 4 评论 0原文

我看过很多关于将相对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-08-26 11:47:32

在 IIS 中,设置虚拟目录 images 并将其指向 c:\my_website_root\images\

如果您的网站已定向到 c:\my_website_root\,则您无需执行任何操作。

In IIS, setup a virtual directory images and point it to c:\my_website_root\images\.

If your website is already directing to c:\my_website_root\, you don't need to do anything.

迷爱 2024-08-26 11:47:32

如果您需要将所有相对 url 转换为绝对 url,请使用此函数:

Private Function ConvertALLrelativeLinksToAbsoluteUri(ByVal html As String, ByVal PageURL As String)
    Dim result As String = Nothing

    ' Getting all Href
    Dim opt As New RegexOptions

    Dim XpHref As New Regex("(href="".*?"")", RegexOptions.IgnoreCase)

    Dim i As Integer
    Dim NewSTR As String = html
    For i = 0 To XpHref.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpHref.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("href=", "").Replace("HREF=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "href=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)


    Next

    html = NewSTR

    Dim XpSRC As New Regex("(src="".*?"")", RegexOptions.IgnoreCase)

    For i = 0 To XpSRC.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpSRC.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("src=", "").Replace("src=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "src=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)


    Next


    Return NewSTR


End Function

If you need to convert all the relative urls to absolute urls use this fucntion:

Private Function ConvertALLrelativeLinksToAbsoluteUri(ByVal html As String, ByVal PageURL As String)
    Dim result As String = Nothing

    ' Getting all Href
    Dim opt As New RegexOptions

    Dim XpHref As New Regex("(href="".*?"")", RegexOptions.IgnoreCase)

    Dim i As Integer
    Dim NewSTR As String = html
    For i = 0 To XpHref.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpHref.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("href=", "").Replace("HREF=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "href=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)


    Next

    html = NewSTR

    Dim XpSRC As New Regex("(src="".*?"")", RegexOptions.IgnoreCase)

    For i = 0 To XpSRC.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpSRC.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("src=", "").Replace("src=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "src=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)


    Next


    Return NewSTR


End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文