How to read custom nodes in Web.config and pay attention to

zhaozj2021-02-16  93

In the past few days, I used the URLMApping class library used to read the custom node in Web.config. After I finished, I feel very good to describe the production process.

Web.config files play a very important role in Web Application. It itself contains a range of settings such as authorization, handler, compilation, globalization, error and tracking. But what should you do when you need some special or want to add your own settings?

To create a custom setting require 2 steps: write CS code and modify web.config

Code Here we have created a simple handler, this handler is used in the web.config file.

using System; using System.Collections; using System.Xml; using System.Configuration; using System.Web.Configuration; namespace Devhood {internal class PageStyleHandler: IConfigurationSectionHandler {public virtual object Create (Object parent, Object context, XmlNode node) {PageStyle config = new PageStyle ((PageStyle) parent); config.LoadValuesFromConfigurationXml (node); return config;}} public class PageStyle {string _backColour; internal PageStyle (PageStyle parent) {if (! parent = null) _backColour = parent._backColour; } internal void LoadValuesFromConfigurationXml (XmlNode node) {XmlAttributeCollection attribCol = node.Attributes; _backColour = attribCol [ "backColour"] Value;.} public string BackColour {get {return _backColour;}}}}

There are two classes here: inheriting the PageStyleHandler class for the IconfigurationSectionHandler interface and the PageStyle class for returning data from WebConfig. The PageStyle class can read an XML node (from Web.config) and save the node data in the BackCOLOUR attribute.

Set web.config file

In order to access the PageStyleHandler, you must do a corresponding configuration on Web.config.

This allows you to access your defined nodes in your web application.

转载请注明原文地址:https://www.9cbs.com/read-12008.html

New Post(0)