C# – Preprocessor directives in Razor

asp.net-mvccpreprocessorrazor

I am writing my first Razor page today, and can't figure out how to enter

#if debug
...
#else
...
#endif

How can I do that in Razor?

Best Answer

I just created an extension method:

public static bool IsDebug(this HtmlHelper htmlHelper)
{
#if DEBUG
      return true;
#else
      return false;
#endif
}

Then used it in my views like so:

<section id="sidebar">
     @Html.Partial("_Connect")
     @if (!Html.IsDebug())
     { 
         @Html.Partial("_Ads")
     }
     <hr />
     @RenderSection("Sidebar", required: false)
</section>

Since the helper is compiled with the DEBUG/RELEASE symbol, it works.