如何为 ASP.Net 网站项目设置不同的构建配置?
根据 有关 Visual Studio 配置的早期问题,无法使用 Visual Studio 的配置管理器为 ASP.net 网站项目创建不同的配置。
对于普通项目,我们有 #if 指令,可以根据我们是在调试还是在生产中切换某些服务器或数据库变量。 这不适用于网站。
例如,在定义网站后端服务器连接的类(在 App_Code 中)中,如果您想在本地运行调试服务器,可能会有这样的一段代码,它会覆盖 web.config 中的生产值machine:
#if DEBUGLOCAL
ServerProperties.ServerIP = "localhost";
ServerProperties.DataContextIP = "localhost";
#endif
这不起作用,因为网站没有“调试本地”配置,因此没有定义 DEBUGLOCAL。
您找到解决此类问题的好方法了吗? 此外(我希望)重构所有内容,以便所有这些引用都存在于类库项目中?
预计到达时间:可以Web 部署项目在这里帮忙?
According to an earlier question about Visual Studio configurations, there's no way to use Visual Studio's configuration manager to create different configurations for an ASP.net web site project.
For normal projects, we have #if directives that switch certain server or database variables depending on whether we're debugging or in production. This doesn't work for web sites.
For example, in a class (in App_Code) that defines a web site's back-end server connection, there might be a chunk of code like this which overrides production values in the web.config if you want to run a debug server on your local machine:
#if DEBUGLOCAL
ServerProperties.ServerIP = "localhost";
ServerProperties.DataContextIP = "localhost";
#endif
This doesn't work, since there's no "Debug Local" configuration for the website, thus no DEBUGLOCAL defined.
Have you found a good way to work around problems like this? Besides (I hope) refactoring everything so all those references live in a class library project?
ETA: Can web deployment projects help here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许在 web.config 中进行一些交换的 Web 部署项目可能会有所帮助? 另一个想法是在 web.config 中有一个连接字符串,它可以从每个环境中存在的预先指定的数据库中提取各种值,以便轻松更改设置,而无需触摸任何文件。
Perhaps a Web Deployment project that does some swapping in the web.config may be of some help? Another thought would be to have a connection string in the web.config that pulls various values from a pre-specified database that can exist in each environment to allow for easy changes to the settings without needing to touch any files.
另一种选择可能是将网站升级为网络应用程序< /a>.
现在进行转换可能需要做大量工作,但如果您决定使用网站或 Web 应用程序,配置的能力可能会帮助您扭转局面。
Another option may be to upgrade the web site to a web application.
That could be a lot of work to make the transition now, but the ability to have configurations may help tip the scales if you're deciding to go with a web site or web application.