Razor view engine, how to enter preprocessor(#if debug)

mamu picture mamu · Jan 14, 2011 · Viewed 68.6k times · Source

I am writing my first razor page today, can't figure out how to enter #if debug #else #endif

How can i enter preprocessor in razor?

Answer

Shawn Wildermuth picture Shawn Wildermuth · Aug 21, 2011

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.