-1

I wand to use a variable with two different path when it is debug and release mode in .cshmtl file

I have tried using #debug and HttpContext.Current.IsDebuggingEnabled but it is not working.

debug is not working in cshtl

I have also tried to implementing @if(Html.IsReleaseBuild())

 @{
    ViewData["Title"] = "Admin Login Page";

      #if DEBUG
           public const bool IS_DEBUG = true;
       #else
           public const bool IS_DEBUG = false;
      #endif

        }


     @if (HttpContext.Current.IsDebuggingEnabled)
        {
    // Debug mode enabled. Your code here. Texts enclosed with <text> tag
       }

    <app>Loading...</app>

    <script src="~/dist/vendor.min.js" asp-append-version="true"></script>
     @section scripts {
        <script src="~/dist/main-client.min.js" asp-append-version="true"> 
          </script>
                   }
Community
  • 1
  • 1
Debashish Dwivedi
  • 327
  • 2
  • 5
  • 13
  • `HttpContext.Current.IsDebuggingEnabled` indicate if the web.config file has the tag `` – asd May 17 '19 at 09:30
  • @asd but httpContext is giving me error. I want to use variable which i can assign on the basis of debug and release – Debashish Dwivedi May 17 '19 at 09:31
  • Add a flag to your model/Viewdata using the controller as per https://stackoverflow.com/questions/42776109/asp-net-core-detecting-debugging-vs-not-debugging-in-a-controller – Steve Land May 17 '19 at 09:31
  • Possible duplicate of [How I can check debug mode in MVC cshtml page](https://stackoverflow.com/questions/46391902/how-i-can-check-debug-mode-in-mvc-cshtml-page) – alireza yazdandoost May 17 '19 at 09:33
  • other solution: https://stackoverflow.com/questions/2951128/c-sharp-and-asp-net-mvc-using-if-directive-in-a-view/8865615#8865615 – asd May 17 '19 at 09:37
  • What are you trying to achieve with the debug/release conditional logic? Perhaps there is a more easier solution, for example using the ASP.NET Core hosting environment. – Henk Mollema May 17 '19 at 09:43
  • Are you *actually* using ASP.NET Core as the tag on your question indicates? ASP.NET Core doesn't use build configurations (techinically, though they can still be utilized via directives in C#) and there's no such thing as `HttpContext.Current` because `HttpContext` is not a static thread local in ASP.NET Core, as it is in ASP.NET. – Chris Pratt May 17 '19 at 13:22
  • @ChrisPratt I checked lots of other solution in which it was mention to do like this. – Debashish Dwivedi May 20 '19 at 04:48
  • @DebashishDwivedi what do you mean by `is not working`? – itminus May 20 '19 at 08:37
  • @itminus I tried using view bag, I accessed property from (ViewBag.IsDebug) and it is working – Debashish Dwivedi May 20 '19 at 09:23
  • @DebashishDwivedi `ViewBag` has no such a `IsDebug` built-in property unless you set it explicitly. – itminus May 20 '19 at 09:32
  • @itminus Yeah I have used explicitly. Thanks for the help – Debashish Dwivedi May 20 '19 at 10:43

1 Answers1

2

You should not declare a public const bool field within the View files.

If you want to check whether it is in Debug/Release mode, you could simple change your code as below :

 @{
    ViewData["Title"] = "Admin Login Page";
    #if DEBUG
        const bool IS_DEBUG = true;
    #else
        const bool IS_DEBUG = false;
    #endif
 }


@if(IS_DEBUG)
{
    <h2>Debug Mode</h2>
}else
{
    <h2>Release Mode</h2>
}

Test Cases

# run in Release mode
dotnet run -c Release

# Or in Debug mode
dotnet run -c Debug
itminus
  • 23,772
  • 2
  • 53
  • 88