Vs2010正式版据说4月12号就要发行,是时候摆弄一下它了 

新建web application后,最直观的一个变化就在于web.config变干净了:

Asp.Net4.0/VS2010新变化(1):web.config与publish
只有寥寥数行代码,赏心悦目!

 

另外注意到web.config在解决方案视图中,分成了二个:

Asp.Net4.0/VS2010新变化(1):web.config与publish

个人觉得这是吸收了ROR中database.yml中数据库(开发,生产,测试)环境配置的思想,我们在做web开发时,难免会对调试/发布二种环境的配置做一些调整,比如:发布环境中连接字符串将连接到正式数据库,调试环境中连接字符串连接到开发数据库,以往的做法是:发布后手动把发布目录下的web.config做一些调整再上传,而现在不需要了!发布时,vs2010能自动根据解决方案设置的环境自动替换相关节点,比如说web.debug.config内容如下:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings  xdt:Transform="Replace">
    <add key="title" value="调试阶段的页面"/>
  </appSettings>
  <system.web>
  </system.web>
</configuration>

 web.release.config内容如下:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <appSettings  xdt:Transform="Replace">
    <add key="title" value="发布后的页面"/>
  </appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

然后可能会在首页后端代码中,这样来设置标题:

protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = ConfigurationManager.AppSettings["title"].ToString();
        }

发布一下:

Asp.Net4.0/VS2010新变化(1):web.config与publish

上图中的"Build configuration:Release",表示发布时将采用release模式,即web.release.config中的AppSetting中的title节点,将自动替换到最终的web.config中,当然你也可以发布成Debug模式,以方便部署到测试环境中,只需要把解决方案属性修改一下,然后再发布即可:
Asp.Net4.0/VS2010新变化(1):web.config与publish

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-11-15
猜你喜欢
  • 2022-02-18
  • 2022-02-17
  • 2021-07-08
  • 2021-06-12
  • 2022-01-27
  • 2021-07-08
  • 2021-11-16
相关资源
相似解决方案