views:

401

answers:

3

I'd be interested to hear how people handle conditional markup, specifically in their masterpages between release and debug builds.

The particular scenario this is applicable to is handling concatenated js and css files. I'm currently using the .Net port of YUI compress to produce a single site.css and site.js from a large collection of separate files.

One thought that occurred to me was to place the js and css include section in a user control or collection of panels and conditionally display the <link> and <script> markup based on the Debug or Release state of the assembly. Something along the lines of:

#if DEBUG
    pnlDebugIncludes.visible = true
#else
    pnlReleaseIncludes.visible = true       
#endif

The panel is really not very nice semantically - wrapping <script> tags in a <div> is a bit gross; there must be a better approach. I would also think that a block level element like a <div> within <head> would be invalid html.

Another idea was this could possibly be handled using web.config section replacements, but I'm not sure how I would go about doing that.

+3  A: 

I just tried this in my Master page in my ASP.NET MVC project and it worked. If in DEBUG mode I use the development version of jQuery and if not in DEBUG mode, I use the minified version of jQuery:

<head runat="server">
<% #if DEBUG %>
    <script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.js") %>"></script>
<% #else %>
    <script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.min.js") %>"></script>
<% #endif %>
</head>
Praveen Angyan
It appears to not work properly in the editor (like in code where the part not in effect is grayed out) but it does work.
Will
+4  A: 
Neil Trodden
I use Development, Test and Production solution configurations as well. As long as you define a conditional compilation symbol for each configuration, you should be able to do something like this:#if Development pnlDevelopmentIncludes.visible = true#endif#if Test pnlTestIncludes.visible = true#endif
Praveen Angyan
Neil this certainly seems like the right approach to take. Could you please expand on how you're using the partial classes to change your markup between different builds?
Bayard Randel
You may want to change your naming convention to be more like web.debub.config or web.release.config. By default IIS will not serve files ending in .config, so it may be better.
Sayed Ibrahim Hashimi
Praveen, that seemed obvious to me initially but the solution configurations cannot be detected using conditional statements as they do not set a symbol. You could edit the properties of your web application and add a release/staging/testing conditional compilation symbol in the build tab - you can set these per build configuration too. I'll add something in the answer.Bayard, I will edit and add a little to my answer to demonstrate.Sayed, good point but I set the build action for those files to 'None' and set them to 'Do Not Copy'. What you say ensures VS handles the file properly.
Neil Trodden
+1  A: 

With regard to the JS files what I do is use Web Deployment Projects to pre-compile the web app. After the build completes if the configuration is Release then I minify the JS files and replace the files in the output directory. This is all done with MSBuild, beacuse Web Deployment Projects are MSBuild files.

It is kind of involved to describe exactly how to do this here, especially if you are not that familar with MSBuild but I cover this exact scenario in my book, Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build.

Sayed Ibrahim Hashim

Sayed Ibrahim Hashimi