asp.net-mvc 在 web.config 文件中创建自定义变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22249672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:34:21  来源:igfitidea点击:

Creating custom variable in web.config file?

asp.net-mvcweb-configconfigwebpage

提问by Chakri

I want to create a variable in web.config file and to use that variable in web forms. How can I achieve this??

我想在 web.config 文件中创建一个变量并在 Web 表单中使用该变量。我怎样才能做到这一点?

回答by DmitryK

in web.config:

在 web.config 中:

<appSettings>
   <add key="message" value="Hello, World!" />
</appSettings> 

in cs:

在 cs 中:

string str = ConfigurationManager.AppSettings["message"].ToString();

回答by Rahul Tripathi

You may try like this:

你可以这样尝试:

<appSettings>
   <add key="id" value="12762"/>
   <add key ="url" value="http://localhost:10982/Redirect.aspx"/>
</appSettings>

Then you can use

然后你可以使用

using System.Configuration;

and use it like this:

并像这样使用它:

string id=ConfigurationManager.AppSettings["Id"].ToString();
string url=ConfigurationManager.AppSettings["Url"].ToString();

回答by AndyRBlank

FYI: The accepted answers for accessing web.config data are considered "Obsolete" now and should be replaced with:

仅供参考:访问 web.config 数据的公认答案现在被视为“过时”,应替换为:

Example:

例子:

in web.config:

在 web.config 中:

<appSettings>
   <add key="message" value="Hello, World!" />
</appSettings>

in c#:

在 C# 中:

ConfigurationManager.AppSettings["message"]

Reference: ConfigurationSettings.AppSettings is obsolete, warning

参考:ConfigurationSettings.AppSettings 已过时,警告