在部署ASP.NET网站时如何处理连接字符串? [英] How do you deal with connection strings when deploying an ASP.NET site?

查看:106
本文介绍了在部署ASP.NET网站时如何处理连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我们的测试和生产数据库位于同一台服务器上,但名称不同。部署意味着编辑Web.config来更改正确数据库的所有连接字符串。我经常忘记的一个步骤...



我们终于创建了一个新的数据库服务器进行测试,我正在移动数据库...但是现在服务器会不同,我们仍然需要处理连接字符串问题。



我正在考虑通过主机文件进行管理,但是当我需要对生产数据进行测试时,在桌面机上进行切换的想法似乎很麻烦。 / p>

所以我只是想知道是否有更好的方法。使用生产Web配置构建部署将是理想的...

解决方案

使用 Web部署项目并更新wdproj文件(它只是一个MSBuild文件)与一些post build任务来输出正确的.config文件。我保留一个web.config和web.release.config然后在wdproj文件中使用它:

 <目标名称= AfterBuild> 
< Copy Condition ='$(Configuration)| $(Platform)'=='Release | AnyCPU'SourceFiles =$(SourceWebPhysicalPath)\web.release.configDestinationFiles =$ )\web.config/>
< Delete Files =$(OutputPath)\web.release.config/>
< / Target>

更多信息



一个更简单的解决方案就是使用 appSources和connectionStrings的configSource属性和然后不要在生产服务器上覆盖该文件。


Right now our test and production databases are on the same server, but with different names. Deploying has meant editing Web.config to change all the connection strings for the correct database. A step which I forget all too frequently...

We've finally created a new database server for testing, and I'm moving the databases over... but now the server will be different and we'll still need to deal with connection string issues.

I was thinking of managing it via a hosts file, but the thought of switching that on my desktop machine whenever I need to test against production data seems cumbersome at best.

So I'm just wondering if there's a better way out there. Something that would build with a "production" web config for deployment would be ideal...

解决方案

Use a Web Deployment Project and update the wdproj file (it's just an MSBuild file) with some post build tasks to output the correct .config file. I keep a web.config and web.release.config then use this in the wdproj file:

<Target Name="AfterBuild">
    <Copy Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " SourceFiles="$(SourceWebPhysicalPath)\web.release.config" DestinationFiles="$(OutputPath)\web.config" />
    <Delete Files="$(OutputPath)\web.release.config" />
</Target>

More information

A simpler solution some like is using configSource property of appSettings and connectionStrings and then never overwriting that file on the production server.

这篇关于在部署ASP.NET网站时如何处理连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆