我已經做了一些代碼,以返回到網站的博客部分的概述頁面。下面代碼中的變量OverviewLink1
和OverviewLink2
都是HtmlAnchor
s,並將設置Href
屬性等於我所做的變量state
。如何在VB中將對象轉換爲字符串變量?
屬性HttpContext.Current.Application
獲取當前HTTP請求的System.Web.HttpApplicationState
對象。
Dim state As HttpApplicationState = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
OverviewLink1.HRef = state ' <-- Error happens on this line
OverviewLink2.HRef = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
此代碼給在第二行上的誤差:HttpApplicationState
類型的
值不能被轉換爲
String
。
第三行不會給出錯誤。 Try it here in this fiddle.現在我有一些問題:
- 第三條線如何工作?
- 在VB中如何投射一般變量的變量?
- 如何預防這種情況?因爲我來自C#,這種情況是不可能的。
我使用VB與ASP.NET Webform (CMS是Liquifi)應用程序。
PS:的由CMS Liquifi使用的變量component.type
,webId
和langId
是變量分別獲取組件(類型String
)的名稱,該網站的ID (一個網站可以有多個外觀)(類型Integer
)和站點(類型Integer
的語言)
更新:
我也試過這個代碼
Dim state As String = HttpContext.Current.Application(component.type & "-" & webId & "-" & langId)
OverviewLink1.HRef = state
OverviewLink2.HRef = state
,這工作得很好但是HttpContext.Current.Application
返回HttpApplicationState
而不是字符串,但有一個鑄鐵發生。爲什麼?
你確定第三行有效嗎? – Pikoh
@Pikoh:是的,我敢肯定,看到小提琴:https://dotnetfiddle.net/deU9N0 –
那個小提琴沒有說什麼,因爲'HttpContext.Current'是'Nothing',所以返回的類型不是'HttpAplicationState' – Pikoh