Topcat:


MS MVP ASP.NET / IIS7 2006~

翻譯工具

 

得獎Link





論壇/討論區:






目前活動連結:

WebMatrix
IADP

修練大會文章總攬:

VS2010
Office2010

計數器與網站工具

Free Page Rank Tool

語言學習

最新回應

[ASP.NET]將相對網址改為完整網址

緣起:

剛好網友遇到這個問題,小喵寫了個小Function來提供轉換

相對網址:

假設目前的路徑是http://www.dotblogs.com.tw/topcat,一般可能遇到的相對網址如以下的幾個

  • 在本來路徑下,子目錄下的另外一個檔案,例如:xxx/test.aspx→http://www.dotblogs.com.tw/topcat/xxx/test.aspx
  • 上層的相對路徑,例如:../xxx/test.aspx→http://www.dotblogs.com.tw/xxx/test.aspx
  • 本專案下的相對路徑,例如~/xxx/test.aspx→http://www.dotblogs.com.tw/topcat/xxx/test.aspx

轉換的Function:

Private Function ChgFullUrl(ByVal p As String) As String
    Dim s As String = ResolveUrl(p)
    Dim ServerName As String = Request.ServerVariables("SERVER_NAME")
    Return "http://" & ServerName & s
End Function

說明:

透過ResolveUrl將相對路徑轉換成真正的路徑,這部份一並解決上層路徑的【..】,相同專案內的【~】,與本來位置的相對子資料夾。

再補上【http://】,以及Server的網址名稱(Request.ServerVariables("SERVER_NAME")),就可以補全完整的路徑了。

 


以下是簽名:

  • 歡迎轉貼本站的文章,不過請在貼文主旨上加上【轉貼】,並在文章中附上本篇的超連結與站名【topcat姍舞之間的極度凝聚】,感恩大家的配合。
  • 如果您覺得此篇對您或者對大家有幫助,敬請按一下【推薦】,感恩!!  ^_^

Microsoft MVP
Microsoft MVP ASP/ASP.NET


topcat
Blog:http://www.dotblogs.com.tw/topcat
 


DotBlogs Tags: ASP.NET

關連文章

[ASP.NET]WebUserControl裡的Button觸發事件,透過Interface取得使用Page內的資料

[ASP.NET]解決GridView使用CheckBox Field遇到【字串未被辨認為有效的Boolean】問題

[ASP.NET]幫Login加上驗證碼

[VS2008]解決移除Office 2007以後VS2008無法切換檢視模式的狀況

如何撰寫包在物件中(FormView,DetailView...)物件(DropDownList,TextBox,....)的事件

ASP.NET Ajax 1.0 初體驗 Part2 (範例:多層式下拉與3+2郵遞區號查詢撰寫錄影檔)

ASP.NET Ajax 1.0 初體驗 (安裝、試用)

安裝完.NET Framework 2.0後才發現IIS沒安裝的問題(無法存取 IIS Metabase)

(VB.NET)將文字20061226轉成日期方式2006/12/26

如何判斷TreeView是否有Node被Selected

回應

  • # re: [ASP.NET]將相對網址改為完整網址 by cloudio

    http://blog.darkthread.net/blogs/darkthreadtw/archive/2010/05/27/6426.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Darkthread+%28Darkthread%29

    上禮拜看到的也不錯,可以省掉判斷https跟port :)

    2010/6/8 下午 03:52 | 回覆

  • # re: [ASP.NET]將相對網址改為完整網址 by player

    這個我之前找過(並把RSS其他的部分小改了一下)

    原出處
    http://aspnetrsstoolkit.codeplex.com/

    改過的
    http://rsstoolkit.codeplex.com/

    RssToolkit.Rss.RssXmlHelper.ResolveAppRelativeLinkToUrl

    http://rsstoolkit.codeplex.com/SourceControl/changeset/view/50548#1189229

    2010/11/3 下午 08:04 | 回覆

登入後使用進階評論

Please add 8 and 7 and type the answer here: