C# AppSettings值在三值运算符中返回null
我已经将一些字符串值移动到我的web配置中,但是,当在三元运算符中用作条件时,有一个值返回null Web配置:C# AppSettings值在三值运算符中返回null,c#,C#,我已经将一些字符串值移动到我的web配置中,但是,当在三元运算符中用作条件时,有一个值返回null Web配置: <add key="Main.Root" value="www.blah.com" /> 代码: 如果我在页面上的任何其他地方使用“AppSettings.SiteRoots.Test”,它将返回正确的值,只有在三元运算符中用作条件时,它才似乎返回null 将三元表达式括在paraenthesis中,同时确保AppSettings.GTMKeys.Test和AppSet
<add key="Main.Root" value="www.blah.com" />
代码:
如果我在页面上的任何其他地方使用“AppSettings.SiteRoots.Test”,它将返回正确的值,只有在三元运算符中用作条件时,它才似乎返回null 将
三元
表达式括在paraenthesis中,同时确保AppSettings.GTMKeys.Test
和AppSettings.GTMKeys.Live
提供了布尔
,它可以与|
一起使用
ViewBag.Profile = HttpContext.IsDebuggingEnabled || (HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live);
您可能不需要在表达式中启用HttpContext.isdebugging
ViewBag.Profile = HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live;
如果你认为三元运算符是问题所在,那么就把它转换成If-else语句,看看它是否消失了。他的意图不是更像
ViewBag.Profile=(…)?测试:实时
?
ViewBag.Profile = HttpContext.IsDebuggingEnabled || (HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live);
ViewBag.Profile = HttpContext.Request.Url.Host == AppSettings.SiteRoots.Test ? AppSettings.GTMKeys.Test : AppSettings.GTMKeys.Live;