【问题标题】:App.config and web.config?App.config 和 web.config?
【发布时间】:2017-04-07 03:09:03
【问题描述】:

我如何从 app.config 和 web.config 中读取数据。

app.config 读取如下。如何从 web.config 读取

 using System.Configuration;

 string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
 string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];

还有我们为什么要存储在这些配置文件中?

我如何从 app.config 下面的用户定义标签<display> 读取??

   <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
       <display>
         <add key="countoffiles" value="7" />
         <add key="logfilelocation" value="abc.txt" />
      </display>
    </configuration>

app.config 和 web.config 中存储的值有什么区别? 我如何决定将某些数据保存在哪里?

当我在 MVC 应用程序的 web.config 中添加 &lt;display&gt; 标签时出现此错误。有任何线索说明原因吗??

【问题讨论】:

    标签: asp.net-mvc web-config app-config


    【解决方案1】:

    Web.Config 用于托管在 IIS(网站、Web 服务)上的应用程序

    App.Config 用于任何其他 .NET 应用程序,例如(WinForms、WPF、Windows 服务)


    要阅读自定义部分,我会这样做:

    NameValueCollection displaySection = (NameValueCollection)ConfigurationManager.GetSection("display");
    string countoffiles = displaySection ["countoffiles"];
    string logfilelocation = displaySection ["logfilelocation"];
    

    关于配置自定义部分,请查看该文章:

    https://msdn.microsoft.com/en-us/library/2tw134k3.aspx


    【讨论】:

    • 我在 web 应用程序中也看到了 app.config。为什么我们要使用它。我们可以在 web 应用程序中添加 app.config 吗
    • 从 web.config 和 app.config 读取有什么区别?code wise
    • 我认为没有任何区别。只是有不同的目的。
    • 另一个疑问,我可以在 web.config 的任何位置添加用户定义的 tag&lt;display&gt; 并读取其值
    猜你喜欢
    • 2010-12-02
    • 2010-11-27
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2013-07-05
    相关资源
    最近更新 更多