[Solution] How to better manage the Web. config for MVC projects

Source: Internet
Author: User
Tags xpath connectionstrings visual studio 2010

Cast Stone asked:

Many times the configuration in Web. config is different in production and PROD environment, a lot of green notes, debug settings and Pord settings mixed together, this you can endure?!

So how to better manage your project's Web. config, OK, let's moving.

Ask the way answer:

During the compilation of the publication, Microsoft Visual Studio 2010 and the following versions are based on Web. config, and then the compilation-based mode (debug or release) for Web.Debug.Config and Web.release . Config to load and eventually generate the corresponding configuration file

1th Step: Add XML Namespaces in Web.Debug.Config and Web.Rlease.Config

<xmlns:xdt= "Http://schemas.microsoft.com/XML-Document-Transform"> 

2nd Step: Learning to use locator feature syntax

2-1 locator= "Condition (XPath expression)"

  <!--Select the add element of its name = = Oldname or ProviderName = = Oldprovider in Web.cong and replace it with this connection string element -  <connectionStrings>    <Addname= "DebugConnection1"connectionString= "Data source=debugsqlserver;initial catalog=debugdb;integrated security=true"ProviderName= "Newprovider"Xdt:locator= "Condition (@name = ' oldname ' or @providerName = ' oldprovider ')"Xdt:transform= "Replace" />  </connectionStrings>

2-2 locator= "Match (comma-delimited list of one or more attribute names)"

 <!--Select the add element of its name = = DebugConnection1 in Web.cong and replace it with this connection string element -  <connectionStrings>    <Addname= "DebugConnection1"connectionString= "Data source=debugsqlserver;initial catalog=debugdb;integrated security=true"ProviderName= "Newprovider"Xdt:locator= "Match (name)"Xdt:transform= "Replace" />  </connectionStrings>


2-3 locator= "XPath (XPath expression)"

 <!--Web.cong Select XPATH = = configuration/connectionstrings[@name = ' DebugConnection1 ' or @providerName = ' System.Data.SqlClient '] element, which is replaced with this add element -<Configurationxmlns:xdt="...">  <connectionStrings>    <Addname= "DebugConnection1"connectionString= "Data source=debugsqlserver;initial catalog=debugdb;integrated security=true"ProviderName= "Newprovider"Xdt:locator= "XPATH (configuration/connectionstrings[@name = ' DebugConnection1 ' or @providerName = ' System.Data.SqlClient ']) "Xdt:transform= "Replace" />  </connectionStrings></Configuration>

The above XPath expression is theresult of combining the implicit XPath condition of the current add element (Configuration/connectionstrings) with an explicitly specified expression

Note The 1:locator feature is optional. If the element's locator attribute is not specified, the default is that this element will find the first element that conforms to the label name of the structure from the top down to perform the set Transform attribute. Examples are as follows:

<!--The entire system.web element is replaced because no Locator attribute is specified to indicate other aspects - <system.webXdt:transform= "Replace">    <customErrorsdefaultredirect= "Genericerror.htm"Mode= "RemoteOnly">    <ErrorStatusCode= "$"redirect= "Internalerror.htm"/>   </customErrors></system.web>

3rd Step learning using Transform feature syntax

Transformation Describe
Xdt:transform= "Replace" Replaces the first node element that matches a locator
Xdt:transform= "Remove" Clears the first node element that matches a locator
Xdt:transform= "RemoveAll" Clears all elements of a matching node tag name
Xdt:transform= "Insert" Adds the specified element to the end of the sibling element
Xdt:transform= "SetAttributes (attributenames)" Finds the specified element and modifies the value of the corresponding property
Xdt:transform= "Removeattributes (attributenames)" Finds the specified element, if there is a specified property, clears the property
Xdt:transform= "InsertBefore (XPath)" Inserts the specified element before specifying an XPath
Xdt:transform= "InsertAfter (XPath)" Inserts the specified element after the specified XPath

Note The 2:locator attribute can be placed in a parent element. Then the collection of its child elements will automatically adapt to the transform attribute that has been set, by default if exist replace, else Add.

Note 3: If the specified element has only the Locator attribute and does not specify any of the Transform attributes, then no operation of the Transform attribute will occur and caution is confused with note 1

Note 4: The Transform attribute on the parent element affects all of its child elements, even if no Transform attribute is specified for the child element, so try not to set the Transform attribute in the parent element .

Reference documents:

Web. config conversion syntax for the deployment of a website application project

ASP. NET 4.0 new features: Web. config transformation

[Solution] how to better manage Web. config for MVC projects

Related Article

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.