Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a Partial view conditionally based on debug or production in ASP.Net MVC site

I have a partial view that includes only basic HTML, no razor code or model. I use this to set up some "guides" for page layout.

What would be the proper/simplest way to only include this partial when the site is run in debug mode?

I know that in my compiled code I could use directives in my C# code to include sections. Is there something similar in razor?

like image 206
John S Avatar asked Mar 03 '14 20:03

John S


1 Answers

You can use HttpContext.Current.IsDebuggingEnabled which would check web.configs' debug setting:

@if(HttpContext.Current.IsDebuggingEnabled) {
    //Do something here.
}

OR use an extension helper method:

public static Boolean DEBUG(this System.Web.Mvc.WebViewPage page) {
// use this sequence of returns to make the snippet ReSharper friendly. 
#if DEBUG
        return true;
#else
        return false;
#endif
}

and usage:

@if(DEBUG()) {
    //debug code here
} else {
    //release code here
}
like image 108
amiry jd Avatar answered Oct 10 '22 10:10

amiry jd