簡體   English   中英

從web.config調用變量以隱藏代碼

[英]Calling variable from web.config to code behind

我想從web.config調用“變量”,然后在其后的代碼中單擊按鈕,然后希望用戶重定向到該變量上的鏈接。 我這樣試過:

web.config中:

<appSettings>
    <add key ="E-leg" value ="https://google.com"/>
</appSettings>

后面的代碼(在按鈕上單擊:

    Protected Sub ELegLogin(sender As Object, e As EventArgs)

        Dim str As string  = ConfigurationManager.AppSettings["E-leg"].ToString();

'Error: Identifier expected

        Response.Redirect(Str)

'Argument not specified for parameter 'Number' of 'Public Function Str(Number As Object) As String
    End Sub
End Class

這不起作用。 我收到錯誤“ 期望的標識符 ”和“ 未為“公共函數Str(作為對象的編號)作為字符串的參數”的參數“編號”指定參數

如果有人可以幫助我,我將不勝感激

您可以嘗試AppSettingsReader

web.config中:

<appSettings>
    <add key ="E-leg" value ="https://google.com"/>
</appSettings>

碼:

Dim reader As New System.Configuration.AppSettingsReader
Response.Redirect(reader.GetValue("E-leg", GetType(String)))

在此處查看更多選項。

您有一個名為str的變量和一個名為Str的函數。 VB.NET是區分大小寫的語言。 如果要重定向到str表示的位置,請確保將變量傳遞給Response.Redirect調用。

Protected Sub ELegLogin(sender As Object, e As EventArgs)
        Dim str As string  = ConfigurationManager.AppSettings["E-leg"].ToString();
        Response.Redirect(str)
End Sub

請注意,命名變量str絕不是一個好主意,因為不清楚它代表什么。 始終給變量一個更語義的名稱,例如eLegRedirectUrl 並且,還要避免使用兩個名稱相似的選項,例如名為str的變量和名為Str的函數。

如果我們假設您的web.config看起來像這樣:

<appSettings>    
     <add key="ClientId" value="234234234"/>    
     <add key ="RedirectUrl" value="http://stackoverflow.com"/>
  </appSettings>

然后您可以添加名稱空間

using System.Configuration;

然后:

string Clientid = ConfigurationManager.AppSettings["ClientId"].ToString();
string Redircturl = ConfigurationManager.AppSettings["RedirectUrl"].ToString();

你很高興。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM