从web.config调用变量到后面的代码

时间:2016-02-16 00:23:31

标签: asp.net vb.net

我想从web.config调用一个“变量”到后面的代码,然后点击一个按钮,我希望用户被重定向到该vaiable上的链接。 我这样试过:

的web.config:

a = scan.nextInt();
 b = scan.nextInt();

后面的代码(点击按钮:

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

这不起作用。我收到错误“标识符预期”和“参数未指定参数'Number''公共函数Str(作为对象的数字)作为字符串

如果有人可以帮助我,我将不胜感激

3 个答案:

答案 0 :(得分:1)

您可以尝试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)))

查看更多选项here

答案 1 :(得分:0)

您有一个名为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的函数。

答案 2 :(得分:-1)

如果我们假设您的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();

你很高兴。

相关问题