简体   繁体   English

C#:从自定义.config文件获取值

[英]C#: Get value from custom .config file

I'd like to get a value from this config file from within an MVC view. 我想从MVC视图中的此配置文件中获取值。 How is this achieved? 如何实现的?

Thanks 谢谢

UnsupportedBrowsers.config (projectRoot/config/..) UnsupportedBrowsers.config (projectRoot / config / ..)

<UnsupportedBrowsers>
  <Browser alias="Internet Explorer">
    <Version>
      <add key="ie6" value="IE6"/>
      <add key="ie7" value="IE7"/>
      <add key="ie8" value="IE8"/>
    </Version>
  </Browser>
</UnsupportedBrowsers>

First, it would be better to do it in the Controller rather than in the View. 首先,最好在Controller中而不是在View中进行。

Second, reading an XML file is an easy task, use XDocument class for example: 其次,读取XML文件是一项容易的任务,例如,使用XDocument类:

var xDoc = XDocument.Load("projectRoot\config\UnsupportedBrowsers.config");
var versionKeys = xDoc.Descendants("Version").First().Descendants();

foreach(var key in versionKeys)
{
  //Do something with the retrived keys..
}

Side note: 边注:

In any case, you're better cache this object in order to avoid of I/O blockings if each new incoming request need to use it. 无论如何,如果每个新的传入请求都需要使用该对象,则最好缓存该对象以避免I / O阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM