繁体   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