[英]Calling variable from web.config to code behind
I would like to call a "variable" from the web.config to the code behind then a button is clicked and I want the user to be redirected to the link that is on that vaiable. 我想从web.config调用“变量”,然后在其后的代码中单击按钮,然后希望用户重定向到该变量上的链接。 I tried it this way: 我这样试过:
web.config: web.config中:
<appSettings>
<add key ="E-leg" value ="https://google.com"/>
</appSettings>
Code behind(on button click: 后面的代码(在按钮上单击:
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
This doesn't work. 这不起作用。 I get error " Identifier expected " and " Argument not specified for parameter 'Number' of 'Public Function Str(Number As Object) As String " 我收到错误“ 期望的标识符 ”和“ 未为“公共函数Str(作为对象的编号)作为字符串的参数”的参数“编号”指定参数
I would appreciate it if someone could help me out 如果有人可以帮助我,我将不胜感激
You could try AppSettingsReader
. 您可以尝试AppSettingsReader
。
web.config: web.config中:
<appSettings>
<add key ="E-leg" value ="https://google.com"/>
</appSettings>
code: 码:
Dim reader As New System.Configuration.AppSettingsReader
Response.Redirect(reader.GetValue("E-leg", GetType(String)))
You have a variable called str
and a function called Str
. 您有一个名为str
的变量和一个名为Str
的函数。 VB.NET is a case sensitive language. VB.NET是区分大小写的语言。 If you want to redirect to the location represented by str
, then make sure you pass that variable to your Response.Redirect
call. 如果要重定向到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
Note that it's never a good idea to name your variable str
as it's not clear what it represents. 请注意,命名变量str
绝不是一个好主意,因为不清楚它代表什么。 Always give your variables a more semantic name, such as eLegRedirectUrl
. 始终给变量一个更语义的名称,例如eLegRedirectUrl
。 And also, avoid having two options with similar names, such as a variable named str
and a function named Str
. 并且,还要避免使用两个名称相似的选项,例如名为str
的变量和名为Str
的函数。
If we assume your web.config looks something like this: 如果我们假设您的web.config看起来像这样:
<appSettings>
<add key="ClientId" value="234234234"/>
<add key ="RedirectUrl" value="http://stackoverflow.com"/>
</appSettings>
Then you could add the namespace 然后您可以添加名称空间
using System.Configuration;
Then: 然后:
string Clientid = ConfigurationManager.AppSettings["ClientId"].ToString();
string Redircturl = ConfigurationManager.AppSettings["RedirectUrl"].ToString();
And you are good to go. 你很高兴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.