我发布了一个网站(MVC前端)和一个API,托管在Azure上的两个不同的域。MVC前端调用API (通过url),但现在,我手动在本地工作和部署时手动切换url:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://myApiDomain.azurewebsites.net"); //https://localhost:44393/
/* I set one URL as
baseaddress and keep
the other in a comment,
depending on environment */现在,它被设置为托管的API地址myApiDomain。所以如果我在本地工作,我会交换这两个地址。当我部署的时候,我放回了azurewebsites url。
另外,我不喜欢将URL直接提交给源代码,而可能是一个配置文件或类似文件-最好是通常被git忽略的文件,这样如果它的源代码是开放的,它就不会显示出来。
做这件事更聪明的方法是什么?
发布于 2021-10-15 10:54:10
如果希望为本地工作或测试和部署编译不同的代码,则可以使用预定义的调试宏。示例:
using System;
namespace debugorrelease
{
class Program
{
static void Main( string[] args )
{
#if DEBUG
Console.WriteLine( "I am in debug mode, which is used for development." );
#else
Console.WriteLine( "I am in release mode, which is used for deployment." );
#endif
}
}
}您可以在visual studio中靠近顶部菜单的调试模式或发布模式下进行设置。

https://stackoverflow.com/questions/69581427
复制相似问题