Friday 17 January 2014

Checking Debug Mode on an MVC View

To check whether our project is in debug/release mode on a particular view, we would not be able to use #if DEBUG #endif on our view because a view is not compiled. To do this, we need to run the logic from the back end. Below is an example of an extension method to check that:
public static bool IsDebug(this HtmlHelper htmlHelper)
{
  #if DEBUG
  return true;
  #else
  return false;
  #endif                  
}

Then on our view, we could call the method:
@if (Html.IsDebug())
{
  @Html.Raw("<span style='color: red'>DEBUGGING MODE IS ON</span>");
}

No comments: