How do I save ASP.NET MVC site settings?

I have an ASP.NET MVC site that consists of 3 projects in a solution:

DomainModel (class library - contains the LINQ repository)
DomainServices (class library - contains the business logic)
WebUI (ASP.NET MVC site)

I need a place to store many settings for our site, which can be configured through XML.

Which project should go? Does anyone have an example of how they load and then access their settings in these different projects?

Do I upload an XML file once in the constructor of some C # class that contains properties for all my settings?

Can someone give me some examples and tips on saving settings in an XML file for use in a multi-project solution?

+5
source share
5 answers

Settings are usually stored in the web.config file. All assemblies have access to these settings.

ConfigurationManager.AppSettings["key"]

You need to add a link to System.configuration.dll

+8
source

Instead of using AppSettings, which is just a collection of key value pairs, consider defining your own configuration structure using ConfigurationSection or IConfigurationSectionHandler

This way you get all the security of the wrapper class and it does not clutter up your AppSettings (and is well defined somewhere for your use).

, XML- XmlSerialization/Deserialization web.config, ( , ).

, web.config, , , -, / .

, -, , /, -...:)

+7

Mathias , . , (, IMHO) - - -, , , :

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

.

, /. XML .

Inverting the Control container will allow you to embed them using dependency injection into other classes.

+1
source

Define the Shared property in your class library to save application settings (instance Specialized.NameValueCollection). Then, in the logic of your site, set this new property in the ASP.NET settings from web.config in the Application_Start application for global.asax.

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

Thus, you can go to the settings from the projects of the class library.

0
source

All Articles