<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MIKE &#187; CSS</title>
	<atom:link href="http://helios.ca/category/development/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://helios.ca</link>
	<description>Just another developer blog</description>
	<lastBuildDate>Tue, 12 Jul 2011 21:35:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET MVC Merge + Minify Javascript on the fly</title>
		<link>http://helios.ca/2011/07/12/asp-net-mvc-merge-minify-javascript-on-the-fly/</link>
		<comments>http://helios.ca/2011/07/12/asp-net-mvc-merge-minify-javascript-on-the-fly/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 21:30:33 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://helios.ca/?p=183</guid>
		<description><![CDATA[According to Yahoo&#8217;s Best Practices for Speeding Up Your Web Site we should make fewer HTTP Requests. I usually like to combines my images into a single *.png file and use CSS Sprites to display the image, or merge all my JavaScripts and Styles into a single js/css file. This article is going to focus only [...]]]></description>
			<content:encoded><![CDATA[<p>According to Yahoo&#8217;s <a href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a> we should make <a href="http://developer.yahoo.com/performance/rules.html#num_http">fewer HTTP Requests</a>.</p>
<p>I usually like to combines my images into a single *.png file and use CSS Sprites to display the image, or merge all my JavaScripts and Styles into a single js/css file.</p>
<p>This article is going to focus only on JavaScript and CSS minification/merging, but I might write another for images and sprites later.</p>
<p>I use this technique in all my project, it is very easy, lightweight and the best is it runs when you build your solution.</p>
<p>In this example I used:</p>
<ul>
<li>Visual Studio 2010</li>
<li>.NET 4</li>
<li>ASP.NET MVC 3</li>
<li>Yahoo YUI Compressor</li>
<li>EcmaScript.NET</li>
</ul>
<h2><strong>Step 1:</strong> Create a new MVC Project.</h2>
<p style="text-align: center;"><a href="http://helios.ca/wp-content/uploads/2011/07/1.png"><img class="size-medium wp-image-343 aligncenter" title="Create MVC Project" src="http://helios.ca/wp-content/uploads/2011/07/1-300x207.png" alt="" width="300" height="207" /></a></p>
<p>&nbsp;</p>
<p style="text-align: center;"><a href="http://helios.ca/wp-content/uploads/2011/07/2.png"><img class="size-medium wp-image-344 aligncenter" title="MVC Project Settings" src="http://helios.ca/wp-content/uploads/2011/07/2-300x269.png" alt="" width="300" height="269" /></a></p>
<h2>Step 2: Create some CSS and JS files.</h2>
<p>In this case I simply split the Site.css that comes with MVC into many files and added a Site.js files that simply does on alert on document ready.</p>
<p style="text-align: center;"><a href="http://helios.ca/wp-content/uploads/2011/07/3.png"><img class="size-full wp-image-345 aligncenter" title="Add some JS and CSS files" src="http://helios.ca/wp-content/uploads/2011/07/3.png" alt="" width="233" height="575" /></a></p>
<h2>Step 3: Modify the MasterPage (_Layout.cshtml) to reference the right files depending on the solution running in Debug or Release.</h2>
<h2><a href="http://helios.ca/wp-content/uploads/2011/07/4.png"><img class="aligncenter size-full wp-image-346" title="MasterPage modifications" src="http://helios.ca/wp-content/uploads/2011/07/4.png" alt="" width="742" height="352" /></a>Step4: Add the EcmacScript.NET and Yahoo.UI.Compressor binariesfrom the example file on Codeplex).</h2>
<h2><a href="http://helios.ca/wp-content/uploads/2011/07/5.png"><img class="aligncenter size-full wp-image-347" title="Add Binaries" src="http://helios.ca/wp-content/uploads/2011/07/5.png" alt="" width="221" height="71" /></a></h2>
<h2>Step 5: Create MSBuild.xml file &#8211; start from Codeplex example</h2>
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
    <UsingTask
        TaskName="CompressorTask"
        AssemblyFile="Yahoo.Yui.Compressor.dll" />

    <PropertyGroup>
        <CssOutputFile Condition=" '$(CssOutputFile)'=='' ">
            ../AssetsMinification/Content/AssetsMinification.css
        </CssOutputFile>
        <JavaScriptOutputFile Condition=" '$(JavaScriptOutputFile)'=='' ">
            ../AssetsMinification/Scripts/AssetsMinification.js
        </JavaScriptOutputFile>
    </PropertyGroup>

    <Target Name="MyTaskTarget">

        <ItemGroup>
            <CssFiles Include="../AssetsMinification/Content/Reset.css"/>
            <CssFiles Include="../AssetsMinification/Content/Site.css"/>
            <CssFiles Include="../AssetsMinification/Content/Text.css"/>
            <CssFiles Include="../AssetsMinification/Content/Forms.css"/>
            <CssFiles Include="../AssetsMinification/Content/Tables.css"/>
            <CssFiles Include="../AssetsMinification/Content/Menu.css"/>

            <JavaScriptFiles Include="../AssetsMinification/Scripts/jquery-1.5.1.js"/>
            <JavaScriptFiles Include="../AssetsMinification/Scripts/modernizr-1.7.js"/>
            <JavaScriptFiles Include="../AssetsMinification/Scripts/Site.js"/>
        </ItemGroup>

        <CompressorTask
            CssFiles="@(CssFiles)"
            DeleteCssFiles="false"
            CssOutputFile="$(CssOutputFile)"
            CssCompressionType="YuiStockCompression"
            JavaScriptFiles="@(JavaScriptFiles)"
            ObfuscateJavaScript="True"
            PreserveAllSemicolons="False"
            DisableOptimizations="Nope"
            EncodingType="Default"
            DeleteJavaScriptFiles="false"
            LineBreakPosition="-1"
            JavaScriptOutputFile="$(JavaScriptOutputFile)"
            LoggingType="ALittleBit"
            ThreadCulture="en-CA"
            IsEvalIgnored="false"
            />
    </Target>
</Project>
</pre>
<h2>Step 5: Run It in release mode</h2>
<p>That&#8217;s it! </p>
<p><a href="http://www.chambaud.com/dump/beta/AssetsMinification.zip">Here&#8217;s the code.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://helios.ca/2011/07/12/asp-net-mvc-merge-minify-javascript-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC and Localization</title>
		<link>http://helios.ca/2009/05/27/aspnet-mvc-and-localization/</link>
		<comments>http://helios.ca/2009/05/27/aspnet-mvc-and-localization/#comments</comments>
		<pubDate>Wed, 27 May 2009 20:50:15 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Developement]]></category>
		<category><![CDATA[Globalization]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://helios.ca/?p=65</guid>
		<description><![CDATA[I live in Montreal &#8211; Canada, and here we have two officials languages, French and English, that&#8217;s why every website I build must be available in both languages. Building an ASP.NET MVC Website using Localization/Globalization is fairly simple and is pretty much like doing it on a plain old ASP.NET Website. We will start this [...]]]></description>
			<content:encoded><![CDATA[<p>I live in Montreal &#8211; Canada, and here we have two officials languages, French and English, that&#8217;s why every website I build must be available in both languages. Building an ASP.NET MVC Website using Localization/Globalization is fairly simple and is pretty much like doing it on a plain old ASP.NET Website.</p>
<p>We will start this example from a blanc ASP.NET MVC Website. This will be easier for you to follow. Now that you have your new ASP.NET MVC Website, create a new class and name it <strong>SetCultureAttribute.cs</strong> in a new <strong>ActionFilter</strong> directory and paste this code inside of it.</p>
<pre class="csharp" name="code">
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.ActionFilter
{
    public class SetCultureAttribute : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext
            filterContext)
        {
            string cultureCode = SetCurrentLanguage(filterContext);

            if (string.IsNullOrEmpty(cultureCode)) return;

            HttpContext.Current.Response.Cookies.Add(
                new HttpCookie("Culture", cultureCode)
                {
                    HttpOnly = true,
                    Expires = DateTime.Now.AddYears(100)
                }
            );

            filterContext.HttpContext.Session["Culture"] = cultureCode;

            CultureInfo culture = new CultureInfo(cultureCode);
            System.Threading.Thread.CurrentThread.CurrentCulture =
                culture;
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                culture;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }

        private static string GetCookieCulture(ActionExecutingContext
            filterContext, ICollection<string> Cultures)
        {
            /* Get the language in the cookie*/
            HttpCookie userCookie = filterContext.RequestContext
                                                .HttpContext
                                                .Request
                                                .Cookies["Culture"];

            if (userCookie != null)
            {
                if (!string.IsNullOrEmpty(userCookie.Value))
                {
                    if (Cultures.Contains(userCookie.Value))
                    {
                        return userCookie.Value;
                    }
                    return string.Empty;
                }
                return string.Empty;
            }
            return string.Empty;
        }

        private static string GetSessionCulture(ActionExecutingContext
            filterContext, ICollection<string> Cultures)
        {
            if (filterContext.RequestContext.HttpContext
                                               .Session["Culture"]
                                                             != null)
            {
                string SessionCulture = filterContext.RequestContext
                                                .HttpContext
                                                .Session["Culture"]
                                                .ToString();

                if (!string.IsNullOrEmpty(SessionCulture))
                {
                    return Cultures.Contains(SessionCulture)
                                 ? SessionCulture
                                 : string.Empty;
                }
                return string.Empty;
            }
            return string.Empty;
        }

        private static string GetBrowserCulture(ActionExecutingContext
            filterContext, IEnumerable<string> Cultures)
        {
            /* Gets Languages from Browser */
            IList<string> BrowserLanguages = filterContext.RequestContext
                                                         .HttpContext
                                                         .Request
                                                         .UserLanguages;

            foreach (var thisBrowserLanguage in BrowserLanguages)
            {
                foreach (var thisCultureLanguage in Cultures)
                {
                    if (thisCultureLanguage != thisBrowserLanguage)
                        continue;

                    return thisCultureLanguage;
                }
            }
            return string.Empty;
        }

        private static string SetCurrentLanguage(ActionExecutingContext
             filterContext)
        {
            IList<string> Cultures = new List<string>
            {
                "en-CA",
                "fr-CA"
            };

            string CookieValue = GetCookieCulture(
                                            filterContext,
                                            Cultures);

            if (string.IsNullOrEmpty(CookieValue))
            {
                string SessionValue = GetSessionCulture(
                                                  filterContext,
                                                  Cultures);

                if (string.IsNullOrEmpty(SessionValue))
                {
                    string BrowserCulture = GetBrowserCulture(
                                                         filterContext,
                                                         Cultures);
                    return string.IsNullOrEmpty(BrowserCulture)
                             ? "en-CA"
                             : BrowserCulture;
                }
                return SessionValue;
            }
            return CookieValue;
        }
    }
}
</pre>
<p>Once this is done, we need to tell our controllers to use this attribute, to do this we could simply go on top of every controller and decorate it with <strong>[SetCulture]</strong> attribute. The down side with this is that we want the complete site multicultural, not just a few controllers or actions. To fix this problem we&#8217;ll make a <strong>BaseController</strong> which will inherit Controller and will be inherited from all our controllers.</p>
<p>To do this, create a new directory and name it <strong>Infrastructure </strong>and then add a new class called <strong>BaseController.cs</strong> in it. This class should contain the following code:</p>
<pre class="csharp" name="code">
using System.Web.Mvc;
using MvcApplication1.ActionFilter;

namespace MvcApplication1.Infrastructure
{
    [SetCulture]
    public class BaseController : Controller
    {
         // Anything you put here will be accessible
         // in every controllers
    }
}
</pre>
<p>As I said before, we need to tell every controllers to use BaseController, so open every controllers and change the inheritance from Controller to BaseController. As of now, your website knows how to handle localization!!! </p>
<p>What we need now, is a way to switch between french and english&#8230; To do this I simply created a new ASP.NET MVC View User Controller (CultureUserControl.ascx) in Views/Shared with the following code.</p>
<pre class="csharp" name="code">
<% if (Session["Culture"].ToString() == "en-CA") {%>
    <a href="/Home/SetCulture/fr-CA">[ Français ]</a>
<% } else if (Session["Culture"].ToString() == "fr-CA") { %>
    <a href="/Home/SetCulture/en-CA">[ English ]</a>
<% } %>
</pre>
<p>Then I added it to the masterpage, right next to the Log On link.</p>
<pre class="csharp" name="code">
<div id="logindisplay">
    <% Html.RenderPartial("LogOnUserControl"); %>
    <% Html.RenderPartial("CultureUserControl"); %>
</div>
</pre>
<p>Last, but not least, add a new <strong>Global.resx</strong> and <strong>Global.fr.rex</strong> in the App_GlobalResources folder. You can start with the welcome message in About &#8211; Index and call it using <strong>Resources.Global.Welcome</strong></p>
<p><img src="http://helios.ca/wp-content/uploads/2009/05/11.jpg" alt="11" title="11" width="547" height="423" class="alignnone size-full wp-image-107" /><img src="http://helios.ca/wp-content/uploads/2009/05/21.jpg" alt="21" title="21" width="547" height="423" class="alignnone size-full wp-image-108" /></p>
<p>You can find a working example of this tutorial <a href="http://helios.ca/wp-content/uploads/2009/05/ASP.NET.MVC.and.ocalization.zip">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://helios.ca/2009/05/27/aspnet-mvc-and-localization/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

