Razor view engine, how to enter preprocessor(#if d

2019-01-03 08:09发布

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?

6条回答
倾城 Initia
2楼-- · 2019-01-03 08:47

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.

查看更多
Juvenile、少年°
3楼-- · 2019-01-03 08:50

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
}
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-03 08:54

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">
}
查看更多
该账号已被封号
5楼-- · 2019-01-03 09:03

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 Code Chief'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!

查看更多
小情绪 Triste *
6楼-- · 2019-01-03 09:04

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>
} 
查看更多
Ridiculous、
7楼-- · 2019-01-03 09:06

C# and ASP.NET MVC: Using #if directive in a view

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.

查看更多
登录 后发表回答