如何创建 web.config 外部的设置文件
我想在 ASP.NET MVC 2.0 项目中创建一个 web.config 外部的设置文件,但我不确定执行此操作的最佳方法。您建议使用哪些方法来创建包含要从源代码管理中排除的敏感信息的设置文件。
I want to create a settings file external to web.config in my ASP.NET MVC 2.0 project and I'm not sure of the best way to go about this. What ways do you recommend for creating a settings file that will contain sensitive information to be excluded from source control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用特殊的
App_Data
文件夹来存储您的设置文件。 ASP.NET 拒绝直接访问此文件夹。就该文件的格式而言,XML 似乎是一种不错的格式。但无论您选择哪种格式,请确保提供强类型 API,允许您访问这些设置,这样您就不会发现自己在控制器操作中解析 XML。在Application_Start
中,您可以解析它并将值存储在某个结构中,该结构可以通过整个应用程序访问。因为您是在 Application_Start 中执行此操作,所以您可以将它与您最喜欢的 DI 框架一起使用,将这些值传递给可能需要它们的所有类,例如控制器和存储库。You could use the special
App_Data
folder to store your settings file. Direct access to this folder is denied by ASP.NET. As far as the format of this file is concerned XML seems a good format. But whatever format you choose make sure you provide a strongly typed API that will allow you to access those settings so that you don't find yourself parsing XML in the middle of a controller action. InApplication_Start
you could parse it and store the values in some structure which will be accessible through the entire application. And because it is in Application_Start that you are doing this you could use it along with your favourite DI framework to pass those values to all classes that might need them such as controllers and repositories.