title: How to use ternary operator in razor (specifically on HTML attributes)?
body: <p>With the WebForms view engine, I'll commonly use the ternary operator for very simple conditionals, especially within HTML attributes. For example:</p>
  
  <pre><code>&lt;a class="&lt;%=User.Identity.IsAuthenticated ? "auth" : "anon" %&gt;"&gt;My link here&lt;/a&gt;
  </code></pre>
  
  <p>The above code will give the <code>&lt;a&gt;</code> tag a class of <code>auth</code> or <code>anon</code> depending on whether the user is authenticated.</p>
  
  <p>What is the equivalent syntax with the Razor view engine? Because Razor requires HTML tags to "know" when to jump in and out of code and markup, I'm currently stuck with the following:</p>
  
  <pre><code>@if(User.Identity.IsAuthenticated)  { &lt;a class="auth"&gt;My link here&lt;/a&gt; }
  else { &lt;a class="anon"&gt;My link here&lt;/a&gt; }
  </code></pre>
  
  <p>This is, to put it mildly, <em>terrible</em>. </p>
  
  <p>I would love to do something <em>like</em> this, but am struggling to understand how in Razor:</p>
  
  <pre><code>&lt;a class="@=User.Identity.IsAuthenticated ? "auth" : "anon";"&gt;My link here&lt;/a&gt;
  </code></pre>
  
  <p>--</p>
  
  <p><strong>Update:</strong></p>
  
  <p>In the meantime, I've created this HtmlHelper:</p>
  
  <pre><code>public static MvcHtmlString Conditional(this HtmlHelper html, Boolean condition, String ifTrue, String ifFalse)
  {
    return MvcHtmlString.Create(condition ? ifTrue : ifFalse);
  }
  </code></pre>
  
  <p>which can be called like this from Razor:</p>
  
  <pre><code>&lt;a class="@Html.Conditional(User.Identity.IsAuthenticated, "auth", "anon")"&gt;My link here&lt;/a&gt;
  </code></pre>
  
  <p>Still, I am hoping there's a way to use the ternary operator without falling back to wrapping it in an extension method.</p>