简体   繁体   English

如何在ASP.NET MVC中使用预定义设置创建.xml文件?

[英]How to create .xml file with predefined settings in ASP.NET MVC?

I have an ASP.NET Application, where I get files from an specific path. 我有一个ASP.NET应用程序,可以从特定路径获取文件。 At the moment the path is a variable in my code: 目前,该路径在我的代码中是一个变量:

string filePath = @"C:\FilesToWatch";

Now my internship boss said, that now I have to predefine the path in an .xml file. 现在我的实习老板说,现在我必须在.xml文件中预定义路径。 My problem is that I do not have really much experience in xml and I do not really know where to store this xml file in my project. 我的问题是我在xml方面没有太多的经验,我也不知道该xml文件在项目中的存储位置。 Has anyone examples or the solution for this? 有没有例子或解决方案?

Your question is a bit unclear, but my guess is that your boss wants this as part of web.config . 您的问题尚不清楚,但是我想您的老板希望将此作为web.config一部分。 It seems the natural place to put any configuration for ASP.NET MVC applications. 似乎可以为ASP.NET MVC应用程序进行任何配置。 This is so you don't need to rebuild and redeploy the whole application if this path changes. 这样一来,如果此路径发生更改,则无需重建和重新部署整个应用程序。 And it has XML format which you mentioned. 它具有您提到的XML格式。

There are numerous ways you can set and retrieve values from the web.config , but the appSettings node is probably the most suited, and so you'd have something along the lines of... 您可以通过多种方法来设置和检索来自web.config值,但是appSettings节点可能是最合适的,因此您可能会遇到一些问题...

<appSettings>
  <add key="path" value="C:\\FilesToWatch" />
</appSettings>

You can access the variable using WebConfigurationManager , thus: 您可以使用WebConfigurationManager来访问变量,从而:

string filePath = WebConfigurationManager.AppSettings["path"];

Further reading: Reading a key from the Web.Config using ConfigurationManager 进一步阅读: 使用ConfigurationManager从Web.Config读取密钥

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

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