简体   繁体   English

AppSettings值在三元运算符中返回null

[英]AppSettings value is returning null in ternary operator

I've been moving some of my string values into my web config, however, one value is returning null when used as a condition in a ternary operator. 我一直将一些字符串值移到Web配置中,但是,当在三元运算符中用作条件时,一个值将返回null。

Web Config: 网络配置:

<add key="Main.Root" value="www.blah.com" />

AppSettings.cs: AppSettings.cs:

public struct SiteRoots
{
   public static readonly string Test = ConfigurationManager.AppSettings["Main.Root"];
}

Code: 码:

 ViewBag.Profile = HttpContext.IsDebuggingEnabled || HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live;

If I use "AppSettings.SiteRoots.Test" anywhere else on the page, it returns the correct value, it only seems to return null when used as a condition inside the ternary operator. 如果在页面上其他任何地方使用“ AppSettings.SiteRoots.Test”,它将返回正确的值,当用作三元运算符中的条件时,它似乎仅返回null。

Enclose the ternary expression in paraenthesis, also ensure AppSettings.GTMKeys.Test and AppSettings.GTMKeys.Live gives boolean to that it could be used with || ternary表达式括在括号中,并确保AppSettings.GTMKeys.TestAppSettings.GTMKeys.Live赋予boolean ,使其可以与||一起使用 .

ViewBag.Profile = HttpContext.IsDebuggingEnabled || (HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live);

You probably do not need HttpContext.IsDebuggingEnabled in your expression 您可能不需要在表达式中使用HttpContext.IsDebuggingEnabled

ViewBag.Profile = HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM