Preprocessor directives in Razor

C#asp.net MvcRazorPreprocessor

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

This is built in to HttpContext:

@if (HttpContext.Current.IsDebuggingEnabled)
{
    // Means that debug="true" in Web.config
}

IMO, this makes more sense than conditional compilation for views and comes in handy for some testing scenarios. (See Tony Wall's comment below.)


Side note: NullReferenceException for HttpContext.Current

Alex Angas mentioned that they get a NullReferenceException with this solution, and a few people have upvoted indicating that this may not be an isolated event.

My best guess: HttpContext.Current is stored in CallContext, meaning it is only accessible by the thread that handles the incoming HTTP request. If your views are being rendered on a different thread (perhaps some solutions for precompiled views?) you would get a null value for HttpContext.Current.

If you get this error, please let me know in the comments and mention if you are using precompiled views or anything special set up that could result in your views being partially rendered/executed on another thread!

"Brief article about using HttpContext.IsDebuggingEnabled"

"Code Chief's comment on this answer"

"Alex Anglas' comment on this answer"

Solution 3 - C#

https://stackoverflow.com/questions/2951128/c-and-asp-net-mvc-using-if-directive-in-a-view/2951200#2951200

Actually that answer has the right answer. You're going to have to pass whether or not you're in debug mode via the Model. (or ViewBag) since all views are compiled in debug mode.

Solution 4 - C#

In .NET Core, you can use the environment tag helper instead of checking the preprocessor variables:

<environment include="Development">
    <!--Debug code here-->
</environment>

Solution 5 - C#

I know this is not a direct answer to the question but as I'm pretty sure debug configuration is corollary to the fact that you are actually executing locally, you can always use the Request.IsLocal property as a debug like test. Thus :

@if (Request.IsLocal)
{
	<link rel="stylesheet" type="text/css" href="~/css/compiled/complete.css">
}
else
{
	<link rel="stylesheet" type="text/css" href="~/css/compiled/complete.min.css">
}

Solution 6 - C#

My solution is very stupid, but it works. Define a global constant somewhere in a static file:

public static class AppConstants
{
#if DEBUG
        public const bool IS_DEBUG = true;
#else
        public const bool IS_DEBUG = false;
#endif
}

Then use it with Razor in HTML:

@if (AppConstants.IS_DEBUG)
{
    <h3>Debug mode</h3>
}
else
{
	<h3>Release mode</h3>
}

Solution 7 - C#

This works for me in a .NET Core 3.0 white label project:

@{
#if CORPA
}
    <button type="button" class="btn btn-warning">A Button</button>
@{
#else
}
    <p>Nothing to see here</p>
@{
#endif
}

Solution 8 - C#

By default MVC views are not compiled so #IF DEBUG can't work in a view. If you want to compile view in order to access IF DEBUG config, you need to :

  1. Right click on your project in Visual Studio
  2. Unload project
  3. Edit project

change the following attribute from false to true

<MvcBuildViews>true</MvcBuildViews>

reload your project and then views are going to be compiled.

The only other work around would be to have a function in your code behind

public static Boolean DEBUG(this System.Web.Mvc.WebViewPage page)
{
   var value = false;
   #if(DEBUG)
       value=true;
   #endif
   return value;
}

and then call it from view :

if(DEBUG())
{
  //debug code here
}
else
{
  //release code here
}

Solution 9 - C#

For me, the code below has worked very well.

When the application is Debugging my buttons appear, when is Release, they don't.

@if (this.Context.IsDebuggingEnabled)
{
    <button type="button" class="btn btn-warning">Fill file</button>
    <button type="button" class="btn btn-info">Export file</button>
} 

Solution 10 - C#

I needed something similar that works in the <script> tag as well, and found that the following works well for either conditional markup in the DOM, or a conditional script.

@{
#if NOEXTAUTH
{
    @:<!-- A single line block of code -->

    <text>
        <!--
        A multi-line block    
        -->
    </text>
}
#endif
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmamuView Question on Stackoverflow
Solution 1 - C#Shawn WildermuthView Answer on Stackoverflow
Solution 2 - C#Jordan GrayView Answer on Stackoverflow
Solution 3 - C#BuildstartedView Answer on Stackoverflow
Solution 4 - C#beleesterView Answer on Stackoverflow
Solution 5 - C#SbuView Answer on Stackoverflow
Solution 6 - C#tedebusView Answer on Stackoverflow
Solution 7 - C#Perry ArmstrongView Answer on Stackoverflow
Solution 8 - C#Yannick RichardView Answer on Stackoverflow
Solution 9 - C#Matheus Fernandes AmorimView Answer on Stackoverflow
Solution 10 - C#Mr MooseView Answer on Stackoverflow