简体   繁体   English

我应该如何存储我的ASP.NET MVC网站的设置?

[英]How should I store my ASP.NET MVC site's settings?

I have an ASP.NET MVC site which is composed of 3 projects in a solution: 我有一个ASP.NET MVC站点,它由一个解决方案中的3个项目组成:

DomainModel (Class Library - Holds LINQ Repo) DomainModel(类库 - 保存LINQ回购)
DomainServices (Class Library - Holds Business Logic) DomainServices(类库 - 包含业务逻辑)
WebUI (ASP.NET MVC Site) WebUI(ASP.NET MVC站点)

I need a place to store a bunch of settings for our site that can be configured via XML. 我需要一个地方来存储我们网站的一系列设置,可以通过XML进行配置。

Which project should this go in? 这应该进入哪个项目? Does anyone have an example of how they load and then access their settings across these different projects? 有没有人有一个例子说明他们如何加载,然后在这些不同的项目中访问他们的设置?

Do I load the XML file once, in the constructor of some C# class that contains properties for all my settings? 我是否在包含所有设置属性的某个C#类的构造函数中加载XML文件一次?

Can someone give me some examples and tips on storing settings in an XML file for use in a multi-project solution? 有人可以给我一些关于在XML文件中存储设置的示例和技巧,以便在多项目解决方案中使用吗?

Settings are usually stored in web.config. 设置通常存储在web.config中。 All assemblies have acces to these settings. 所有程序集都可以访问这些设置。

ConfigurationManager.AppSettings["key"]

You need to add a reference to System.configuration.dll 您需要添加对System.configuration.dll的引用

Rather than using AppSettings, which is just a collection of key value pairs, consider defining your own configuration structure using ConfigurationSection or IConfigurationSectionHandler 而不是使用AppSettings,它只是键值对的集合,而是考虑使用ConfigurationSection或IConfigurationSectionHandler定义自己的配置结构

That way you get all the safety of the wrapper class, and it doesn't clutter up your AppSettings (and is nicely defined somewhere for your use). 这样你就可以获得包装类的所有安全性,并且它不会使你的AppSettings混乱(并且在某个地方很好地定义供你使用)。

Even better, define your own XML schema, and use XmlSerialization/Deserialization to store this file outside of web.config, listening for changes on it (tie it to the cache, whatever). 更好的是,定义自己的XML模式,并使用XmlSerialization / Deserialization将此文件存储在web.config之外,监听它的更改(将其绑定到缓存,无论如何)。

If you do it this way, you don't need to modify web.config to get the changes and therefore don't need to restart your web application losing session/cache in the process. 如果以这种方式执行此操作,则无需修改web.config以获取更改,因此无需重新启动Web应用程序,从而在此过程中丢失会话/缓存。

Not that well written stateless web applications should care about losing session/cache - but there is always somebody... :) 不是写得好的无状态Web应用程序应该关心丢失会话/缓存 - 但总有人...... :)

I agree with Mathias to use the default way offered by the framework. 我同意Mathias使用框架提供的默认方式。 However, you can (should, IMHO) still make some kind of wrapper class, probably behind an interface for facilitating mocking in unit tests, eg: 但是,您可以(应该,恕我直言)仍然制作某种包装类,可能在一个界面后面,以便在单元测试中进行模拟,例如:

class MyAppSettings
{
    public int SomeSetting 
    { 
       get 
       {
         return Convert.ToInt32(ConfigurationManager.AppSettings["SomeSetting"]); 
       } 
    }
}

I would store it in a new service with an interface. 我会将它存储在带有界面的新服务中。

What I do is have a class have all the properties/settings. 我所做的是拥有一个具有所有属性/设置的类。 It would also manage the XML file. 它还将管理XML文件。

An Inversion of Control container would allow you to injection them using dependency injection into your other classes. 控制容器的反转将允许您使用依赖注入将它们注入到其他类中。

Define a Shared property in your class library to hold the app settings (an instance of Specialized.NameValueCollection ). 在类库中定义共享属性以保存应用程序设置( Specialized.NameValueCollection的实例)。 Then in your site logic, set this new property to your ASP.NET settings from web.config in Application_Start sub of global.asax. 然后在您的站点逻辑中,将此新属性从global.asax的Application_Start sub中的web.config设置为ASP.NET设置。

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup
    DomainModel.Common.AppSettings = ConfigurationManager.AppSettings
End Sub

This way, you can get to the settings from the class library projects. 这样,您就可以从类库项目中获取设置。

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

相关问题 我应该将我的网站的ASP.NET身份数据库与我的网站的“其他数据”数据库分开吗? - Should I have my site's ASP.NET Identity database separate from my site's “other data” database? 我应该在哪里存储我的临时视图模型在 asp.net MVC 中? - Where should i store my temporary view model in asp.net MVC? 我可以将设置存储在ASP.NET MVC中的settings.config文件中吗? - Can I store settings in a settings.config file in ASP.NET MVC? 如何判断我的网站是否运行ASP.NET MVC或Web窗体? - How Can I Tell If My Site Is Running ASP.NET MVC or Web Forms? 如何识别从其他地方返回我的ASP.NET MVC网站的用户? - How do I identify a user returning to my ASP.NET MVC site from elsewhere? 如何在ASP.NET MVC3网站上对我的Json结果进行单元测试? - How can I unit test my Json result in an ASP.NET MVC3 web site? 如何让SSIS作业与ASP.NET MVC网站通信? - How can I have an SSIS job communicate with my ASP.NET MVC site? 如何将 AdSense 代码添加到我的 ASP.NET MVC 网站? - How can I add AdSense code to my ASP.NET MVC site? 如何为我的 asp.net MVC 站点的每个访问者添加 cookie? - How do I add a cookie for every visitor to my asp.net MVC site? 我应该如何构建我的视图模型 - ASP.NET MVC - How should i structure my View Model - ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM