Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.mvc view enteres #IF DEBUG in release configuration

I have an ASP MVC view where have the following statement

#if DEBUG
  //section 1
  //do stuff
#else
  //section 2
  //do other stuff
#endif

When in visual studio I choose the release configuration from the dropdown to do the build, the code still steps through section 1.

In the solution configuration properties all subprojects of the solution are set to the release configuration.

What am I not getting here?

like image 631
Sjors Miltenburg Avatar asked Sep 04 '09 11:09

Sjors Miltenburg


People also ask

How do I view Cshtml in my browser?

Right click the Index. cshtml file and select View in Browser. You can also right click the Index. cshtml file and select View in Page Inspector.


1 Answers

It is important to understand that there are two, entirely separate, compilations for your project. The first is the one you do in Visual Studio. The second is the one which ASP.NET does just before the page is served. The if DEBUG inside your view is done in the second step. The release build you describe is the first step. Therefore, your project's debug/release setting has nothing at all to do with the debug setting in Web.config/the ASP.NET compiler.

Moreover, it would be completely inappropriate for your Visual Studio build to change the debug setting in the Web.config. These are two separate compilations, and one should not affect the other.

On the other hand, you probably have a perfectly reasonable need for making your view behave differently when you are debugging inside of Visual Studio, and you can do this. You just need to move the "if" statement outside of the view and into something which is compiled by Visual Studio, instead of ASP.NET. We do this with an HTML helper. For example:

        /// <summary>
        /// Returns the HTML to include the appropriate JavaScript files for 
        /// the Site.Master.aspx page, depending upon whether the assembly 
        /// was compiled in debug or release mode.
        /// </summary>
        /// <returns>HTML script tags as a multi-line string.</returns>
        public static string SiteMasterScripts(this UrlHelper helper)
        {
            var result = new StringBuilder();
#if DEBUG
            result.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>", helper.Content("~/Content/js/MicrosoftAjax.debug.js"));
#else
            result.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>", helper.Content("~/Content/js/MicrosoftAjax.js"));
#endif
        // etc. ...

This includes debug JS files when running in debug configuration in Visual Studio, but minimized JS otherwise.

like image 55
Craig Stuntz Avatar answered Oct 09 '22 23:10

Craig Stuntz