在web.config文件中创建自定义变量?

Cha*_*kri 36 asp.net-mvc webpage config web-config

我想在web.config文件中创建一个变量,并在Web表单中使用该变量.我怎么能实现这个?

Dmi*_*ryK 68

在web.config中:

<appSettings>
   <add key="message" value="Hello, World!" />
</appSettings> 
Run Code Online (Sandbox Code Playgroud)

在cs中:

string str = ConfigurationManager.AppSettings["message"].ToString();
Run Code Online (Sandbox Code Playgroud)


Rah*_*thi 14

你可以尝试这样:

<appSettings>
   <add key="id" value="12762"/>
   <add key ="url" value="http://localhost:10982/Redirect.aspx"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

然后你可以使用

using System.Configuration;
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

string id=ConfigurationManager.AppSettings["Id"].ToString();
string url=ConfigurationManager.AppSettings["Url"].ToString();
Run Code Online (Sandbox Code Playgroud)