我有各種網頁需要建立一個URL來顯示或放置在發出的電子郵件中。我繼承的代碼具有名爲FixedConstants的公共類中的Public Const中的Web服務器名稱的值。例如:從類中調用時HttpContext.Current.Request.UserHostName爲空
Public Const cdServerName As String = "WEBSERVERNAME"
試圖改善這一點,我寫了這個:
Public Class UIFunction
Public Shared myhttpcontext As HttpContext
Public Shared Function cdWebServer() As String
Dim s As New StringBuilder("http://")
Dim h As String
h = String.Empty
Try
h = Current.Request.ServerVariables("REMOTE_HOST").ToString()
Catch ex As Exception
Dim m As String
m = ex.Message.ToString() 'Ignore this should-not-occur thingy
End Try
If h = String.Empty Then
h = "SomeWebServer"
End If
s.Append(h)
s.Append("/")
Return s.ToString()
End Function
我已經嘗試不同的東西,而作爲HttpContext.Current.Request.UserHostName調試這樣,我總是得到一個空字符串,它抽出我的默認字符串「SomeWebServer」。
我知道Request.UserHostName或Request.ServerVariables(「REMOTE_HOST」)從頁面調用時起作用,但爲什麼從類文件的調用方法(即UIFunction.vb)調用時返回空?