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

时间:2014-02-26 09:05:25

标签: c#

我一直在将我的一些字符串值移动到我的web配置中,但是,当一个值在三元运算符中用作条件时,返回null。

Web配置:

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

AppSettings.cs:

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

代码:

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

如果我在页面的任何其他位置使用“AppSettings.SiteRoots.Test”,它将返回正确的值,当用作三元运算符内的条件时,它似乎只返回null。

1 个答案:

答案 0 :(得分:1)

ternary表达式包含在paenthesis中,同时确保AppSettings.GTMKeys.TestAppSettings.GTMKeys.Liveboolean ||它可以与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;

你的表达式中可能不需要HttpContext.IsDebuggingEnabled

{{1}}