前言

    本文主要阐述一些Visual Studio开发下需要知道的少部分且比较实用的功能,也是很多人忽略的部分。一些不常用而且冷门的功能不在本文范围,当然本文的尾巴【.Net必知系列】纯属意淫,如有雷同,基情四射。。

目录
  1. WEB项目版本发布时 Web.config 中 Web.Release.config 和 Web.Debug.config 的使用。
  2. 项目属性下【生成事件】使用。
  3. 快速分析未知架构和类调用结构的方式(VS2010/2012)。
一:Web.Release.config 和 Web.Debug.config 的使用

    有时候我们开发与发布Web应用程序时Web.Config中的配置可能不一样,比如数据库连接字符串。那在发布时想自动替换Web.config文件的内容如何办呢?特别是项目开发到后期配置节点越来越多,共同部分和差异部分经常使用<!--注释--> 来进行节点的切换,这样是不是觉得太麻烦了,现在我们就来解决这个问题,当然这只是演示比较常用的一种方式,至于其他方式不做言表。

  Visual Studio (VS IDE) 你必须知道的功能和技巧 - 【.Net必知系列】大家看见的这个就是所说的3个经常看见但是下面2个一般没有人使用额配置文件, Web.Release/Debug.config内容描叙(默认新建项目会自动生成)

下面是Web.Release.config 和 Web.Debug.config 的配置文件

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an attribute "name" that has a value of "MyDB".
      -->
  <connectionStrings>
    <add name="AppConnection"
      connectionString="这里填写【测试环境连接字符串】或【正式环境的字符串】分别写在Release.config,Debug.config中"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

</configuration>

下面是Web.config 的配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
    <connectionStrings>
    <add name="AppConnection" connectionString="默认连接字符串"/>
    <add name="TestCommon" connectionString="公共使用的连接字符串"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>


注意,我在Web.config的connectionStrings节点下 下建立2个 子节点用来比较发布后的效果,当发布后就会变成

 下面是发布 后Web.config 的配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <connectionStrings>//注意这里的字符串变了,变成了release配置文件中的字符串
    <add name="AppConnection" connectionString="发布环境连接字符串"/>
    <add name="TestCommon" connectionString="公共使用的连接字符串"/>
  </connectionStrings>
  <system.web>
    <compilation targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

这样就自动把替换Web.config中需要替换的节点按照设定的模式进行处理了,下面具体看一下描叙语言吧。

    这里面2句重要的语句是 xdt:Transform="SetAttributes" xdt:Locator="Match(name)" 这2个配置属性意思是,把Web.config中connectionStrings对应name为AppConnection的节点属性替换。

  这里的语法来源于xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" 命名空间,那2个分开的文件中都需要导入才能使用

    简单讲一下常用配置属性

1 :locator属性

(1)Match;

这里你需要就是在你直接匹配的属性名。    

<connectionStrings>
<add name="Northwind" connectionString="connection string detail"
    providerName="System.Data.SqlClient"
    xdt:Transform="Replace"
    xdt:Locator="Match(name)" />
</connectionStrings>
View Code

 

相关文章:

  • 2022-01-28
  • 2021-12-15
  • 2021-06-05
  • 2021-06-01
  • 2021-12-31
  • 2021-08-12
  • 2021-09-09
猜你喜欢
  • 2021-12-06
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案