如何基于解决方案配置而非环境变量配置startup.cs? [英] How to configure startup.cs based on solution configuration and not environment variable?
问题描述
配置startup.cs的一种方法是根据环境变量检查环境,然后在Configure方法中运行相应的代码:
One way of configuring startup.cs is to check the environment based on an environmental variable, and run the appropriate code within the Configure method:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
对于正在使用的应用程序,我不能保证可以访问客户端计算机以添加ASPNETCORE_ENVIRONMENT环境变量,以确保将它们路由到正确的异常页面.但是我也不想一直要记住要在发布项目时注释掉某些代码.
For the app I'm working on, I can't guarantee that I will have access to clients' machines to add the ASPNETCORE_ENVIRONMENT environmental variable to make sure they're routed to the correct exception page. But I also don't want to keep having to remember to comment out certain code when I'm about to publish the project.
我该如何获取项目的配置名称来确定要运行的代码?:
How can I instead get the project's configuration name to determine what code I want run?:
以下是我想要完成的操作的伪代码:
Here's some pseudocode of what I'd like to accomplish:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//I get the configuration name (could be "Debug", "Development", or "Release)
//from the list in the photo
string configurationName = GetConfigurationName();
//I compare the name to determine what error page to show
if (configurationName == "Debug" || configurationName == "Development")
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
推荐答案
If you can't make it work with the environment, you could use a preprocessor directive. When deploying your code to production, make sure you are compiling with RELEASE as solution configuration:
#if DEBUG
app.UseDeveloperExceptionPage();
#elif RELEASE
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
#endif
您可以找到一些不错的示例这里:
You can find some nice examples here:
这篇关于如何基于解决方案配置而非环境变量配置startup.cs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!