Web.config custom Node

Source: Internet
Author: User
Tags cdata

References: About Configurationmanager.getsection () method

First build an MVC project, the project name is Mvcapp, and then create a custom class under the Models folder of the project. The class I created here is named ContactUsSectionHandler.cs, which inherits from the IConfigurationSectionHandler interface. The function of this class is to obtain the content ContactUsSectionHandler.cs class of this contactus node in the Web.config configuration file

Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Linq;
Using System.Web;

Using System.Xml; Namespace Mvcapp.models {public class Contactussectionhandler:iconfigurationsectionhandler {private Xm
        Lnode _sectionnode;
        Public XmlNode Sectionnode {get {return _sectionnode;} ///When the Configurationmanager.getsection ("Contactussection") method is invoked, if a class inherits the IConfigurationSectionHandler interface, The Create method for this interface is triggered, at which point Configurationmanager.getsection ("Contactussection") The Contactussection node in the Web.config configuration file that is obtained is passed to the third parameter section of the Create method. So what's saved in this section is the contactussection of this node in the Web,config configuration file. Public virtual object Create (object paarnet, Object con Text, XmlNode section)//Implement the Create method in IConfigurationSectionHandler interface {this._sectionnode = section;//Initial To _sectionnode the private member return of this; This is actually the object of the MvcApp.Models.ContactUsSectionHandler class. Because I already have the contactu in the Web.config configuration file in the Create methodSsection This node assigns a value to a private property of the current class Contactussectionhandler _sectionnode.
        So here you can return an object of the current class. }

    }
}

The class that receives the Contactussection node in the Web.config configuration file is well defined. Here's a custom contactussection node web.cofig configuration file in web.config.

<?xml version= "1.0" encoding= "Utf-8"?> <!--for more information about how to configure the ASP.net application, visit http://go.microsoft.com/fwlink/? linkid=169433--> <configuration> <configsections > <!--since you want to customize a contactussection node, you need to start with the <co Nfigsections></configsections> This node to declare the name and type of the custom node--> <!--This type indicates what the type of node is, and it actually means that if you want to get the node, Which class to use to hold the contents of this contactussection node. I use the Contactussectionhandler class under the Models folder under the Mvcapp project to save the contents of the Contactussection node. The mvcapp behind the comma is the project name--> <section name= "contactussection" type= "Mvcapp.models.contactussectionhandler,mvcapp"/ > </configSections> <appSettings> <add key= "webpages:version" value= "2.0.0.0"/> <add key= "webpages:enabled" value= "false"/> <add key= "Preserveloginurl" value= "true"/> <add key= "ClientVa" Lidationenabled "value=" true "/> <add key=" unobtrusivejavascriptenabled "value=" true "/>

  T <system.web>  

After receiving the class, and the configuration file is defined, see how we can use it to create the home controller

Using Mvcapp.models;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;

Namespace Mvcapp.controllers
{public
    class Homecontroller:controller
    {////Get
        :/home/ Public

        ActionResult Index ()
        {
            ///Call Configurationmanager.getsection ("Contactussection") method to get the contents of the Contactussection node in the Web.config configuration file.
            //Note: The contents of the Contactussection node in the Web.config configuration file are saved in the private properties _sectionnode of the Contactussectionhandler class
            //Note: _ The type of the Sectionnode property is XmlNode type, so it can save the contents of the Contactussection node
            contactussectionhandler section = System.Configuration.ConfigurationManager.GetSection ("Contactussection") as Contactussectionhandler;

            return View ();}}




============================================================ good. We're looking at how to use the information under the Contactussection node or initialize a class

First, we create a user class under the Models folder.

User class

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;

Namespace Mvcapp.models
{public
    class User
    {public
        string Name {get; set;}

        public string Age {get; set;}

        public string Gender {get; set;}

        There are many things you can do in this class
    }

And then ContactUsSectionHandler.cs the code to write the initialization user class.

Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Linq;
Using System.Web;

Using System.Xml; Namespace Mvcapp.models {public class Contactussectionhandler:iconfigurationsectionhandler {private Xm
        Lnode _sectionnode;
        Public XmlNode Sectionnode {get {return _sectionnode;} ///When the Configurationmanager.getsection ("Contactussection") method is invoked, if a class inherits the IConfigurationSectionHandler interface, The Create method for this interface is triggered, at which point Configurationmanager.getsection ("Contactussection") The Contactussection node in the Web.config configuration file that is obtained is passed to the third parameter section of the Create method. So what's saved in this section is the contactussection of this node in the Web,config configuration file. Public virtual object Create (object paarnet, Object con Text, XmlNode section)//Implement the Create method in IConfigurationSectionHandler interface {this._sectionnode = section;//Initial To _sectionnode the private member return of this; This is actually the object of the MvcApp.Models.ContactUsSectionHandler class. Because I already have the contactu in the Web.config configuration file in the Create methodSsection This node assigns a value to a private property of the current class Contactussectionhandler _sectionnode.
        So here you can return an object of the current class. //Because there are other child nodes under the Contactussection node, each child node will have its own function.
            Now let's write an example//Get an instance of the User class, actually get a list<user> object public list<user> getuserinstance () {
            list<user> list = new list<user> ();
            Initializeuser (list);
        return list;
            }//Initialize User public void Initializeuser (list<user> List) {XmlNode node = null;
                foreach (XmlNode chilnode in _sectionnode) {//If the name of the child node is this node of user if (chilNode.Name.ToLower () = = "User".
                    ToLower ()) {node = Chilnode;
                Break }///Traverse User This node foreach (XmlNode childnode in node.)
                ChildNodes) {User user = new user (); User. Name = childnode.attributes["NAMe "].
                Value; User. Age = childnode.attributes[' age '].
                Value; User. Gender = childnode.attributes["Gender"].
                Value; List.
            ADD (user); }
        }

    }
}
After writing the code, we come to the home controller to invoke the
Using Mvcapp.models;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;

Namespace Mvcapp.controllers
{public
    class Homecontroller:controller
    {////Get
        :/home/ Public

        ActionResult Index ()
        {
            ///Call Configurationmanager.getsection ("Contactussection") method to get the contents of the Contactussection node in the Web.config configuration file.
            //Note: The contents of the Contactussection node in the Web.config configuration file are saved in the private properties _sectionnode of the Contactussectionhandler class
            //Note: _ The type of the Sectionnode property is XmlNode type, so it can save the contents of the Contactussection node
            contactussectionhandler section = System.Configuration.ConfigurationManager.GetSection ("Contactussection") as Contactussectionhandler;

            list<user> User = section. Getuserinstance ();

            return View ();}}

We can see that the user has three items


The above is just an example. Of course, we can do a lot of things with nodes.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.