<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Imran Baloch's Blog</title><link>http://weblogs.asp.net/imranbaloch/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Creating a simple Live Cricket Score using ASP.NET SignalR</title><link>http://weblogs.asp.net/imranbaloch/archive/2013/05/11/creating-a-simple-live-cricket-score-using-asp-net-signalr.aspx</link><pubDate>Sat, 11 May 2013 14:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10272909</guid><dc:creator>imran_ku07</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=10272909</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2013/05/11/creating-a-simple-live-cricket-score-using-asp-net-signalr.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I am from Pakistan. Like most of Pakistanis, I am also a huge fan of Cricket. Yesterday, I got some time from my busy&amp;nbsp;job.&amp;nbsp;I thought to create a simple Cricket Live Score feature using ASP.NET SignalR. It is also Interesting to note that the guys(David Fowler from West Indies&amp;nbsp;and Damian Edwards from Australia) that are heavily involved in SignalR development are also cricket fans(correct me if I am wrong).&amp;nbsp;&amp;nbsp;In this article, I will show&amp;nbsp;you how to create a simple 'Live Cricket Score' feature using SignalR. For&amp;nbsp;a quick demo, just open &lt;a href="http://109.73.74.20/CricketDemo/pakistan-vs-india.html" mce_href="http://109.73.74.20/CricketDemo/pakistan-vs-india.html"&gt;this link&lt;/a&gt; in&amp;nbsp;different browsers and in one browser just append&amp;nbsp;&lt;em&gt;?admin=&lt;/em&gt;&amp;nbsp;query-string(which will become the admin who update the score), then just click &lt;em&gt;Update Score&lt;/em&gt; button many times until 2 overs finish(the match will be limited&amp;nbsp;to 2 overs). For each click, the live score will be updated in&amp;nbsp;each browser.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img style="width: 785px; height: 687px;" border="1" align="middle" src="http://weblogs.asp.net/blogs/imranbaloch/Cricket.png" width="785" height="687" mce_src="http://weblogs.asp.net/blogs/imranbaloch/Cricket.png"&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If you are new to SignalR then &lt;a href="http://www.asp.net/signalr" mce_href="http://www.asp.net/signalr"&gt;Getting Started&lt;/a&gt; will be great for you. Assuming that you have configured your application with ASP.NET SignalR.&amp;nbsp;Let create a simple html page with following lines,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;        
        &amp;lt;!DOCTYPE html&amp;gt;
        &amp;lt;html&amp;gt;
        &amp;lt;head&amp;gt;
            &amp;lt;title&amp;gt;Pakistan v/s India&amp;lt;/title&amp;gt;
            &amp;lt;style&amp;gt;
                .team
                {
                    font-size: 15px;
                    padding-right: 10px;
                    font-weight: bold;
                    text-transform: capitalize;
                }
                .wickets,overs
                {
                    padding-right: 5px;
                }
            &amp;lt;/style&amp;gt;
        &amp;lt;/head&amp;gt;
        &amp;lt;body&amp;gt;
            &amp;lt;h1&amp;gt;Pakistan v/s India&amp;lt;/h1&amp;gt;
            &amp;lt;div id="updateScore" style="display: none;"&amp;gt;
                &amp;lt;input type="button" id="updateScoreLink" value="Update Score"/&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;div data-bind="foreach: teams"&amp;gt;
                &amp;lt;div&amp;gt;
                    &amp;lt;span class="team" data-bind="{ html: team }"&amp;gt;&amp;lt;/span&amp;gt;
                    &amp;lt;span class="runs" data-bind="{ html: runs }"&amp;gt;&amp;lt;/span&amp;gt;/&amp;lt;span class="wickets" data-bind="{ html: wickets }"&amp;gt;&amp;lt;/span&amp;gt;
                    &amp;lt;b&amp;gt;Overs&amp;lt;/b&amp;gt; &amp;lt;span class="overs" data-bind="{ html: oversText }"&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;script src="Scripts/jquery-1.8.2.min.js" &amp;gt;&amp;lt;/script&amp;gt;
            &amp;lt;script src="Scripts/jquery.signalR-1.0.1.min.js"&amp;gt;&amp;lt;/script&amp;gt;
            &amp;lt;script src="signalr/hubs"&amp;gt;&amp;lt;/script&amp;gt;
            &amp;lt;script src="Scripts/knockout-2.2.0.js" &amp;gt;&amp;lt;/script&amp;gt;
            &amp;lt;script type="text/javascript"&amp;gt;
                // Here we start
            &amp;lt;/script&amp;gt;
        &amp;lt;/body&amp;gt;
        &amp;lt;/html&amp;gt;
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In the above page,&amp;nbsp;we have&amp;nbsp;referenced jquery, signalr and knockout scripts.&amp;nbsp;We have also define a simple template which will show the runs, wickets&amp;nbsp;and overs of both teams. For demo purpose,&amp;nbsp;we have&amp;nbsp;added a&amp;nbsp;&lt;em&gt;Update Score&lt;/em&gt; button in the page. This button when clicked&amp;nbsp;will&amp;nbsp;record&amp;nbsp;a single ball of an over&amp;nbsp;as well as&amp;nbsp;runs scored in this ball . After each ball, I will tell&amp;nbsp;SignalR to broadcast teams score-card across all the connected client. When the match finished(after 24 balls), I will again tell the SignalR to show the final result to all users. Here are the scripts which are required for our work, &lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;  
        $(function () {
            var chat = $.connection.cricketHub,
                viewModel = {
                    teams: ko.observableArray([
                    { team: 'india', runs: 0, wickets: 0, balls: 0, oversText: '0.0' },
                    { team: 'pakistan', runs: 0, wickets: 0, balls: 0, oversText: '0.0'}])
                },
                index = 0;// index represant the position of the current batting team
            chat.client.updateScore = function (t) {
                viewModel.teams(JSON.parse(t));
            };
            chat.client.matchFinished = function (result) {
                alert(result);
            };
            $.connection.hub.start().done(function () {
                $('#updateScoreLink').click(function () {
                    var teams = viewModel.teams(),
                        team1 = teams[index],
                        team2 = teams[(index + 1) % 2],
                        wicketFalled = getRandomInt(0, 5) == 3 ? true : false;
                    // Assuming that the wicket will only fall when  a 0-5 random number is 3
                    team1.runs += getRandomInt(0, 6); // Get a random score between 0 to 6
                    team1.wickets += wicketFalled ? 1 : 0;
                    team1.balls += 1;
                    team1.oversText = truncate(team1.balls / 6).toString() + '.' + (team1.balls % 6).toString();
                    chat.server.updateScore(JSON.stringify(teams));
                    if (team1.balls === 12 &amp;amp;&amp;amp; team2.balls === 12) {
                        $('#updateScoreLink').hide();
                        var message = (team1.runs &amp;gt; team2.runs ? team1.team : team2.team) + ' won';
                        message = team1.runs === team2.runs ? 'match tied' : message;
                        message += '\n' + team1.team + ': ' + team1.runs + '/' + team1.wickets + ' overs: ' + team1.oversText;
                        message += '\n' + team2.team + ': ' + team2.runs + '/' + team2.wickets + ' overs: ' + team2.oversText;
                        chat.server.matchFinished(message);
                    }
                    else if (team1.balls === 12) {
                        index = 1;// second team batting
                    }
                    viewModel.teams([team1,team2]);
                });
            });
            ko.applyBindings(viewModel);
            if (isAdmin()) {
                $('#updateScore').show();
            }
            
            function getRandomInt(min, max) {
                return Math.floor(Math.random() * (max - min + 1)) + min;
            }
            function truncate(n) {
                return Math[n &amp;gt; 0 ? "floor" : "ceil"](n);
            }
            function isAdmin(){
                // For demo, assuming that only admin can update the score 
                // and the admin will be user which have admin querystring.
                return location.href.indexOf('admin=') &amp;gt; -1
            }
        });
&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In the above scripts,&amp;nbsp;we are&amp;nbsp;initializing SignalR hub cricketHub object(we will see this hub class shortly),&amp;nbsp;viewModel object which include team information(runs, wickets, etc) and index which represent the current batting team. Next, we have two client functions(updateScore and matchFinished) which will be invoked when the server broadcast a message to all clients.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;When the hub start, we are registering event handler&amp;nbsp;&lt;em&gt;Update Score&lt;/em&gt; click event where we are getting a random run between 0-6, a&amp;nbsp;wicket or not depending upon a random number, updating&amp;nbsp;our view-model and then telling SignalR to broadcast the view-model across all the connected clients so that all clients automatically update their scorecard. Finally, when both teams&amp;nbsp;completed their 12 balls, we will send the results to all the connected clients using SignalR. Note that for demo purpose we are only showing the &lt;em&gt;Update Score&lt;/em&gt; button to the user which have &lt;em&gt;admin=&lt;/em&gt; query-string.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Finally, here is&amp;nbsp;our CricketHub class,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        
    public class CricketHub : Hub
    {
        public void UpdateScore(string teams)
        {
            Clients.All.UpdateScore(teams);
        }
        public void MatchFinished(string result)
        {
            Clients.All.MatchFinished(result);
        }
    }
&lt;/pre&gt;
&lt;br&gt;



&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you one of&amp;nbsp;the usage of ASP.NET SignalR. You have seen that how easily we can create a&amp;nbsp;real-time Live Cricket Score using the power of ASP.NET SignalR which works in every browser .&amp;nbsp;Hopefully you will enjoy my this article too.&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10272909" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/SignalR/default.aspx">SignalR</category></item><item><title>A small but crucial point about App_Start folder</title><link>http://weblogs.asp.net/imranbaloch/archive/2013/04/30/a-small-but-crucial-point-about-app-start-folder.aspx</link><pubDate>Wed, 01 May 2013 03:29:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10255906</guid><dc:creator>imran_ku07</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=10255906</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2013/04/30/a-small-but-crucial-point-about-app-start-folder.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&amp;nbsp;&lt;/p&gt;&lt;p mce_keep="true"&gt;As we know that IIS reject requests which contain folder started with App_ (specifically App_code, App_GlobalResources, App_LocalResources, App_WebReferences, App_Data, App_Browsers folders). Lot of developers put some important&amp;nbsp;files here so that only the application code can access these files and nobody can access these files directly.&amp;nbsp;With the introduction of ASP.NET MVC 4, a new folder App_Start has been&amp;nbsp;included in the application template. But there is a chance that developers&amp;nbsp;put important files in App_Start folder without realizing that this folder is &lt;strong&gt;&lt;font size="3"&gt;not&lt;/font&gt;&lt;/strong&gt; protected by IIS request filtering module. So, I just want to warn the developers that don't confuse App_* folders with&amp;nbsp;App_Start folder.&amp;nbsp; App_*(see the&amp;nbsp;list above) folders are protected by IIS and App_Start folder is not.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10255906" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Security/default.aspx">Security</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/IIS/default.aspx">IIS</category></item><item><title>Localizing Default Error Messages in ASP.NET MVC and WebForm</title><link>http://weblogs.asp.net/imranbaloch/archive/2013/03/31/localizing-default-error-messages-in-asp-net-mvc-and-web-form.aspx</link><pubDate>Sun, 31 Mar 2013 10:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10075154</guid><dc:creator>imran_ku07</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=10075154</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2013/03/31/localizing-default-error-messages-in-asp-net-mvc-and-web-form.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.ComponentModel.DataAnnotations validation attributes (like &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx"&gt;RequiredAttribute&lt;/a&gt;) allows us to localize&amp;nbsp;error messages using &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessageresourcename.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessageresourcename.aspx"&gt;ErrorMessageResourceName&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessageresourcetype.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessageresourcetype.aspx"&gt;ErrorMessageResourceType&lt;/a&gt;. But some error messages cannot be localized using these properties. For example, '&lt;em&gt;The value X is not valid for&amp;nbsp;Y'&lt;/em&gt;.&amp;nbsp;&amp;nbsp;In this article, I will quickly show you how to localize these error messages in ASP.NET MVC and ASP.NET WebForm.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For localizing default error messages in ASP.NET MVC we&amp;nbsp;will set these&amp;nbsp;below&amp;nbsp;properties in Global.asax Application_Start method,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        
        ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
        DefaultModelBinder.ResourceClassKey = "MyResources";
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next, create a MyResources.resx resource file and/or your culture specific resource file MyResources.xx.resx inside App_GlobalResources folder where&amp;nbsp;we can override &lt;em&gt;FieldMustBeDate&lt;/em&gt;, &lt;em&gt;FieldMustBeNumeric&lt;/em&gt;, &lt;em&gt;PropertyValueInvalid&lt;/em&gt; and &lt;em&gt;PropertyValueRequired&lt;/em&gt; messages. Here are the default values, &lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;  
        FieldMustBeDate The field {0} must be a date.
        FieldMustBeNumeric The field {0} must be a number.
        PropertyValueInvalid The value '{0}' is not valid for {1}.
        PropertyValueRequired A value is required.
&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For WebForm, we need to localize &lt;em&gt;PropertyValueInvalid&lt;/em&gt; and &lt;em&gt;PropertyValueRequired&lt;/em&gt;. But here we need to do some more work. Just add a class MyErrorMessageProvider.cs which do all the work for us(Note: most code are copied from System.Web.dll),&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        
        public class MyErrorMessageProvider
    {
        private static string _resourceClassKey;

        public static void SetResourceClassKey(string resourceClassKey){
            if(resourceClassKey == null){
                throw new ArgumentNullException("resourceClassKey");
            }
            _resourceClassKey = resourceClassKey;
            ModelBinderErrorMessageProviders.ValueRequiredErrorMessageProvider = MyErrorMessageProvider.DefaultValueRequiredErrorMessageProvider;
            ModelBinderErrorMessageProviders.TypeConversionErrorMessageProvider = MyErrorMessageProvider.DefaultTypeConversionErrorMessageProvider;
        }

        private static string DefaultTypeConversionErrorMessageProvider(ModelBindingExecutionContext modelBindingExecutionContext, ModelMetadata modelMetadata, object incomingValue)
        {
            return GetResourceCommon(modelBindingExecutionContext, modelMetadata, incomingValue, new Func&amp;lt;ModelBindingExecutionContext, string&amp;gt;(GetValueInvalidResource));
        }

        private static string DefaultValueRequiredErrorMessageProvider(ModelBindingExecutionContext modelBindingExecutionContext, ModelMetadata modelMetadata, object incomingValue)
        {
            return GetResourceCommon(modelBindingExecutionContext, modelMetadata, incomingValue, new Func&amp;lt;ModelBindingExecutionContext, string&amp;gt;(GetValueRequiredResource));
        }

        private static string GetResourceCommon(ModelBindingExecutionContext modelBindingExecutionContext, ModelMetadata modelMetadata, object incomingValue, Func&amp;lt;ModelBindingExecutionContext, string&amp;gt; resourceAccessor)
        {
            string displayName = modelMetadata.GetDisplayName();
            string str = resourceAccessor(modelBindingExecutionContext);
            object[] objArray = new object[2];
            objArray[0] = incomingValue;
            objArray[1] = displayName;
            string str1 = string.Format(CultureInfo.CurrentCulture, str, objArray);
            return str1;
        }

        private static string GetUserResourceString(ModelBindingExecutionContext modelBindingExecutionContext, string resourceName, string resourceClassKey)
        {
            if (string.IsNullOrEmpty(resourceClassKey) || modelBindingExecutionContext == null || modelBindingExecutionContext.HttpContext == null)
            {
                return null;
            }
            else
            {
                return modelBindingExecutionContext.HttpContext.GetGlobalResourceObject(resourceClassKey, resourceName, CultureInfo.CurrentUICulture) as string;
            }
        }

        private static string GetUserResourceString(ModelBindingExecutionContext modelBindingExecutionContext, string resourceName)
        {
            return GetUserResourceString(modelBindingExecutionContext, resourceName, _resourceClassKey);
        }

        private static string GetValueInvalidResource(ModelBindingExecutionContext modelBindingExecutionContext)
        {
            string userResourceString = GetUserResourceString(modelBindingExecutionContext, "PropertyValueInvalid");
            return userResourceString;
        }

        private static string GetValueRequiredResource(ModelBindingExecutionContext modelBindingExecutionContext)
        {
            string userResourceString = GetUserResourceString(modelBindingExecutionContext, "PropertyValueRequired");
            return userResourceString;
        }
    }

&lt;/pre&gt;
&lt;br&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and then we can set our resource class file at Global.asax Application_Start method,&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp;&lt;/p&gt;
&lt;br&gt; &lt;br&gt;
&lt;pre class="brush:csharp"&gt;    MyErrorMessageProvider.SetResourceClassKey("MyResources");
&lt;/pre&gt;
&lt;br&gt; &lt;br&gt;
&lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you how easily we can localize error messages in ASP.NET MVC and ASP.NET WebForm&amp;nbsp;. Hopefully you will enjoy my this article too.&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10075154" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Web+Form/default.aspx">Web Form</category></item><item><title>A Simple jQuery HttpClient</title><link>http://weblogs.asp.net/imranbaloch/archive/2013/02/25/a-simple-jquery-httpclient.aspx</link><pubDate>Mon, 25 Feb 2013 19:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9913076</guid><dc:creator>imran_ku07</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=9913076</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2013/02/25/a-simple-jquery-httpclient.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; One of the reason why HttpClient getting popular day by day is that it is very simple to use&amp;nbsp;and intuitive programming model. Due its simplicity, I have created a very simple HttpClient for jQuery(which abstract the jQuery.ajax calls) for myself and my friends, so that the programming model remain unified. But I think/feel that&amp;nbsp;this may be useful and interesting for others. That's why I&amp;nbsp;have created a package to share this library.&amp;nbsp;In this article I will tell you how to use this simple jQuery HttpClient.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First of all just install &lt;a href="https://www.nuget.org/packages/jquery.httpclient" mce_href="https://www.nuget.org/packages/jquery.httpclient"&gt;jquery.httpclient&lt;/a&gt; package from nuget and then include this in your view(or page).&amp;nbsp;Now you can&amp;nbsp;send ajax request&amp;nbsp;in&amp;nbsp;HttpClient library&amp;nbsp;fashion. Here are examples that you can do using this library,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:js"&gt;        
        var client = new HttpClient();


        // Send a GET request
        client.getAsync('/Home/B');


        // Send a GET request with done callback
        client.getAsync('/Home/B', function (response) {
            console.log('Get', response);
        });


        // Send a GET request with done and fail callback
        client.getAsync('/Home/B', function (response) {

        },
        function (err) {

        });


        // Send a DELETE request
        client.deleteAsync('/Home/B');


        // Send a DELETE request with done callback
        client.deleteAsync('/Home/B', function (response) {
            console.log('Get', response);
        });


        // Send a DELETE request with done and fail callback
        client.deleteAsync('/Home/B', function (response) {

        },
        function (err) {

        });


        // Send a POST request with data
        client.postAsync('/Home/A', 'year=2008');


        // Send a POST request with data and done callback
        client.postAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a POST request with data, done and fail callback
        client.postAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data and content-type
        client.postAsync('/Home/A', 'year=2008', 'application/json');


        // Send a POST request with data, content-type and done callback
        client.postAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a POST request with data, content-type, done and fail callback
        client.postAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data, content-type and accept
        client.postAsync('/Home/A', 'year=2008', 'application/json', 'application/json');


        // Send a POST request with data, content-type, accept and done callback
        client.postAsync('/Home/A', 'year=2008', 'application/json', 'application/json', function (response) {
        });


        // Send a POST request with data, content-type, accept, done and fail callback
        client.postAsync('/Home/A', 'year=2008', 'application/json', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data and application/json content-type
        client.postAsJsonAsync('/Home/A', 'year=2008');


        // Send a POST request with data, application/json content-type and done callback
        client.postAsJsonAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a POST request with data, application/json content-type, done and fail callback
        client.postAsJsonAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data, application/json content-type and accept
        client.postAsJsonAsync('/Home/A', 'year=2008', 'application/json');


        // Send a POST request with data, application/json content-type, accept and done callback
        client.postAsJsonAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a POST request with data, application/json content-type, accept, done and fail callback
        client.postAsJsonAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data and application/xml content-type
        client.postAsXmlAsync('/Home/A', 'year=2008');


        // Send a POST request with data, application/xml content-type and done callback
        client.postAsXmlAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a POST request with data, application/xml content-type, done and fail callback
        client.postAsXmlAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a POST request with data, application/xml content-type and accept
        client.postAsXmlAsync('/Home/A', 'year=2008', 'application/json');


        // Send a POST request with data, application/xml content-type, accept and done callback
        client.postAsXmlAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a POST request with data, application/xml content-type, accept, done and fail callback
        client.postAsXmlAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data
        client.putAsync('/Home/A', 'year=2008');


        // Send a PUT request with data and done callback
        client.putAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a PUT request with data, done and fail callback
        client.putAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data and content-type
        client.putAsync('/Home/A', 'year=2008', 'application/json');


        // Send a PUT request with data, content-type and done callback
        client.putAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a PUT request with data, content-type, done and fail callback
        client.putAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data, content-type and accept
        client.putAsync('/Home/A', 'year=2008', 'application/json', 'application/json');


        // Send a PUT request with data, content-type, accept and done callback
        client.putAsync('/Home/A', 'year=2008', 'application/json', 'application/json', function (response) {
        });


        // Send a PUT request with data, content-type, accept, done and fail callback
        client.putAsync('/Home/A', 'year=2008', 'application/json', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data and application/json content-type
        client.putAsJsonAsync('/Home/A', 'year=2008');


        // Send a PUT request with data, application/json content-type and done callback
        client.putAsJsonAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a PUT request with data, application/json content-type, done and fail callback
        client.putAsJsonAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data, application/json content-type and accept
        client.putAsJsonAsync('/Home/A', 'year=2008', 'application/json');


        // Send a PUT request with data, application/json content-type, accept and done callback
        client.putAsJsonAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a PUT request with data, application/json content-type, accept, done and fail callback
        client.putAsJsonAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data and application/xml content-type
        client.putAsXmlAsync('/Home/A', 'year=2008');


        // Send a PUT request with data, application/xml content-type and done callback
        client.putAsXmlAsync('/Home/A', 'year=2008', function (response) {
        });


        // Send a PUT request with data, application/xml content-type, done and fail callback
        client.putAsXmlAsync('/Home/A', 'year=2008', function (response) {
        },
        function (err) {

        });


        // Send a PUT request with data, application/xml content-type and accept
        client.putAsXmlAsync('/Home/A', 'year=2008', 'application/json');


        // Send a put request with data, application/xml content-type, accept and done callback
        client.putAsXmlAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        });


        // Send a PUT request with data, application/xml content-type, accept, done and fail callback

        client.putAsXmlAsync('/Home/A', 'year=2008', 'application/json', function (response) {
        },
        function (err) {

        });        
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In&amp;nbsp;the above example&amp;nbsp;we are sending&amp;nbsp;GET, POST, PUT and DELETE request in similar&amp;nbsp;way that we do with&amp;nbsp;HttpClient library. The source code of simple jQuery HttpClient is available at &lt;a href="https://github.com/imranbaloch/jquery.httpclient" mce_href="https://github.com/imranbaloch/jquery.httpclient"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you a simple&amp;nbsp;jQuery HttpClient which help you to send ajax request in HttpClient library fashion. I have done this for myself for making the programming model unified in javascript with HttpClient library. Hopefully you will enjoy my this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9913076" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NEt+Web+API/default.aspx">ASP.NEt Web API</category></item><item><title> Microsoft MVP for year 2013</title><link>http://weblogs.asp.net/imranbaloch/archive/2013/01/01/microsoft-mvp-for-year-2013.aspx</link><pubDate>Tue, 01 Jan 2013 16:24:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9684503</guid><dc:creator>imran_ku07</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=9684503</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2013/01/01/microsoft-mvp-for-year-2013.aspx#comments</comments><description>&lt;p style="text-align: center;"&gt;&lt;img border="0" src="http://weblogs.asp.net/blogs/imranbaloch/mvp.jpg" width="178" height="72" mce_src="http://weblogs.asp.net/blogs/imranbaloch/mvp.jpg"&gt;&lt;/p&gt;

&lt;p&gt;Just got an email from Microsoft that I'm being nominated for Microsoft Most Valuable
Professional (Microsoft MVP) for the thrid consecutive year. Thanks to Microsoft and&amp;nbsp;all my friends. &amp;nbsp; &lt;br&gt;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9684503" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>ASP.NET Bundling/Minification and Embedded Resources</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/12/29/asp-net-bundling-and-minification-and-embedded-resources.aspx</link><pubDate>Sat, 29 Dec 2012 13:30:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9652826</guid><dc:creator>imran_ku07</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=9652826</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/12/29/asp-net-bundling-and-minification-and-embedded-resources.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If you want to share your application resources(like css, javascript, images, etc) between different projects then embedded resource&amp;nbsp;is a&amp;nbsp;great choice. Embedded resource&amp;nbsp;is also&amp;nbsp;good for component/control writers because it allows&amp;nbsp;component/control writers to distribute all the application resources with just a single assembly.&amp;nbsp;A lot of vendors are already using this approach.&amp;nbsp;It will great for component/control writers if they can leverage the ASP.NET bundling and minification for improving the performance. So, in this article I will show you how to write&amp;nbsp;a very basic component(helper) which&amp;nbsp;will use the&amp;nbsp;ASP.NET bundling and minification feature on&amp;nbsp;embedded javascript/css&amp;nbsp;files.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First of all create a new Class Library project and install the &lt;a href="http://nuget.org/packages/Microsoft.AspNet.Mvc" mce_href="http://nuget.org/packages/Microsoft.AspNet.Mvc"&gt;Microsoft.AspNet.Mvc&lt;/a&gt;, &lt;a href="http://nuget.org/packages/WebActivator" mce_href="http://nuget.org/packages/WebActivator"&gt;WebActivator&lt;/a&gt;&amp;nbsp;and  &lt;a href="http://nuget.org/packages/Microsoft.AspNet.Web.Optimization/1.1.0-alpha1" mce_href="http://nuget.org/packages/Microsoft.AspNet.Web.Optimization/1.1.0-alpha1"&gt;Microsoft ASP.NET Web Optimization Framework 1.1.0-alpha1&lt;/a&gt;(make sure to include the&amp;nbsp;-Pre parameter in Package Manager Console) nuget packages. Next, add a reference of System.Web assembly. Then, create&amp;nbsp;your control/component/helper. For demonstration purpose, I will use this sample helper,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        public static class HtmlHelpers
        {
            public static MvcHtmlString NewTextBox(this HtmlHelper html, string name)
            {
                var js = Scripts.Render("~/ImranB/Embedded/Js").ToString();
                var css = Styles.Render("~/ImranB/Embedded/Css").ToString();
                var textbox = html.TextBox(name).ToString();
                return MvcHtmlString.Create(textbox + js + css);
            }
	}
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In this helper, I am just using a textbox with a style and script bundle. Style bundle include 2&amp;nbsp;css files&amp;nbsp;and script bundle include 2 js files.&amp;nbsp;So, just create 2 css files(NewTextBox1.css and NewTextBox2.css) and 2 javascript files(NewTextBox1.js and NewTextBox2.js) and then mark these files as embedded&amp;nbsp;resource. Next, add a AppStart.cs file and&amp;nbsp;add the following lines inside this file,&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        [assembly: WebActivator.PostApplicationStartMethod(typeof(AppStart), "Start")]
        namespace ImranB
        {
            public static class AppStart
            {
                public static void Start()
                {
                    ConfigureRoutes();
                    ConfigureBundles();
                }
                private static void ConfigureBundles()
                {
                    BundleTable.VirtualPathProvider = new EmbeddedVirtualPathProvider(HostingEnvironment.VirtualPathProvider);
                    BundleTable.Bundles.Add(new ScriptBundle("~/ImranB/Embedded/Js")
                        .Include("~/ImranB/Embedded/NewTextBox1.js")
                        .Include("~/ImranB/Embedded/NewTextBox2.js")
                        );

                    BundleTable.Bundles.Add(new StyleBundle("~/ImranB/Embedded/Css")
                        .Include("~/ImranB/Embedded/NewTextBox1.css")
                        .Include("~/ImranB/Embedded/NewTextBox2.css")
                        );
                }
                private static void ConfigureRoutes()
                {
                    RouteTable.Routes.Insert(0,
                        new Route("ImranB/Embedded/{file}.{extension}",
                            new RouteValueDictionary(new { }),
                            new RouteValueDictionary(new { extension = "css|js" }),
                            new EmbeddedResourceRouteHandler()
                        ));
                }
            }
        }
&lt;/pre&gt;
&lt;p mce_keep="true"&gt;. &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;The above class&amp;nbsp;using &lt;a href="http://blogs.msdn.com/b/davidebb/archive/2010/10/11/light-up-your-nupacks-with-startup-code-and-webactivator.aspx" mce_href="http://blogs.msdn.com/b/davidebb/archive/2010/10/11/light-up-your-nupacks-with-startup-code-and-webactivator.aspx"&gt;WebActivator's PostApplicationStartMethod&lt;/a&gt;, which allows your assembly to run some code after the&amp;nbsp;Application_Start method&amp;nbsp;of&amp;nbsp;global.asax. The Start method simply register a custom virtual path provider and&amp;nbsp;two bundles which are used in our NewText helper class. But ASP.NET optimization framework will only emit a bundle url&amp;nbsp;when debug="false" or when BundleTable.EnableOptimizations = true.&amp;nbsp;Therefore, we also need to handle the normal javascript and css requests. For this case, the above method&amp;nbsp;has also register a specific route for handling embedded resource requests using EmbeddedResourceRouteHandler route handler.&amp;nbsp;Here is the definition of this handler, &lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:csharp"&gt;	
        public class EmbeddedResourceRouteHandler : IRouteHandler
        {
            IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
            {
                return new EmbeddedResourceHttpHandler(requestContext.RouteData);
            }
        }
        public class EmbeddedResourceHttpHandler : IHttpHandler
        {
            private RouteData _routeData;
            public EmbeddedResourceHttpHandler(RouteData routeData)
            {
                _routeData = routeData;
            }
            public bool IsReusable
            {
                get { return false; }
            }
            public void ProcessRequest(HttpContext context)
            {
                var routeDataValues = _routeData.Values;
                var fileName = routeDataValues["file"].ToString();
                var fileExtension = routeDataValues["extension"].ToString();
                string nameSpace = typeof(EmbeddedResourceHttpHandler)
                                    .Assembly
                                    .GetName()
                                    .Name;// Mostly the default namespace and assembly name are same
                string manifestResourceName = string.Format("{0}.{1}.{2}", nameSpace, fileName, fileExtension);
                var stream = typeof(EmbeddedResourceHttpHandler).Assembly.GetManifestResourceStream(manifestResourceName);
                context.Response.Clear();
                context.Response.ContentType = "text/css";// default
                if (fileExtension == "js")
                    context.Response.ContentType = "text/javascript";
                stream.CopyTo(context.Response.OutputStream);
            }
        }
&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;EmbeddedResourceRouteHandler returns EmbeddedResourceHttpHandler http handler which&amp;nbsp;will be&amp;nbsp;used to extract embedded resource file from the assembly and then write the file to the response body. Now, the only missing thing is EmbeddedVirtualPathProvider,&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        public class EmbeddedVirtualPathProvider : VirtualPathProvider
        {
            private VirtualPathProvider _previous;
            public EmbeddedVirtualPathProvider(VirtualPathProvider previous)
            {
                _previous = previous;
            }
            public override bool FileExists(string virtualPath)
            {
                if (IsEmbeddedPath(virtualPath))
                    return true;
                else
                    return _previous.FileExists(virtualPath);
            }
            public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
            {
                if (IsEmbeddedPath(virtualPath))
                {
                    return null;
                }
                else
                {
                    return _previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
                }
            }
            public override VirtualDirectory GetDirectory(string virtualDir)
            {
                return _previous.GetDirectory(virtualDir);
            }
            public override bool DirectoryExists(string virtualDir)
            {
                return _previous.DirectoryExists(virtualDir);
            }
            public override VirtualFile GetFile(string virtualPath)
            {
                if (IsEmbeddedPath(virtualPath))
                {
                    string fileNameWithExtension = virtualPath.Substring(virtualPath.LastIndexOf("/") + 1);
                    string nameSpace = typeof(EmbeddedResourceHttpHandler)
                                    .Assembly
                                    .GetName()
                                    .Name;// Mostly the default namespace and assembly name are same
                    string manifestResourceName = string.Format("{0}.{1}", nameSpace, fileNameWithExtension);
                    var stream = typeof(EmbeddedVirtualPathProvider).Assembly.GetManifestResourceStream(manifestResourceName);
                    return new EmbeddedVirtualFile(virtualPath, stream);
                }
                else
                    return _previous.GetFile(virtualPath);
            }
            private bool IsEmbeddedPath(string path)
            {
                return path.Contains("~/ImranB/Embedded");
            }
        }
    public class EmbeddedVirtualFile : VirtualFile
    {
        private Stream _stream;
        public EmbeddedVirtualFile(string virtualPath, Stream stream)
            : base(virtualPath)
        {
            _stream = stream;
        }
        public override Stream Open()
        {
            return _stream;
        }
    } 
&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmbeddedVirtualPathProvider class is self explanatory. It simply&amp;nbsp;maps&amp;nbsp;a bundling url(used above) and return the embedded request as&amp;nbsp;stream.&amp;nbsp;Note, this class will be invoked by ASP.NET optimization&amp;nbsp;framework during bundling and minifying process. Now, just build your component/control/helper assembly.&amp;nbsp;Then, create a sample ASP.NET(MVC) application and&amp;nbsp;then use this component/control/helper&amp;nbsp;in your page. For example, like,&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;        @using ImranB.Helpers
        
        @Html.NewTextBox("New")
&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you how to create a component/control/helper that leverage the ASP.NET Optimization framework. A sample application is available at &lt;a href="https://github.com/imranbaloch/ASP.NET-Optimization-Framework-and-Embeded-Resource" mce_href="https://github.com/imranbaloch/ASP.NET-Optimization-Framework-and-Embeded-Resource"&gt;github&lt;/a&gt; for download. Hopefully you will enjoy my this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9652826" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Html+Helper/default.aspx">Html Helper</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Css/default.aspx">Css</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Bundling+and+Minification/default.aspx">Bundling and Minification</category></item><item><title>Fixing the Model Binding issue of ASP.NET MVC 4 and ASP.NET Web API</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/12/08/fixing-model-the-binding-issue-of-asp-net-mvc-and-asp-net-web-api.aspx</link><pubDate>Sat, 08 Dec 2012 13:27:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9535665</guid><dc:creator>imran_ku07</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=9535665</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/12/08/fixing-model-the-binding-issue-of-asp-net-mvc-and-asp-net-web-api.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Yesterday when I was checking ASP.NET forums, I found an important&amp;nbsp;&lt;a href="http://forums.asp.net/t/1856357.aspx/1?FormValueProvider+skips+form+values" mce_href="http://forums.asp.net/t/1856357.aspx/1?FormValueProvider+skips+form+values"&gt;issue&lt;/a&gt;/&lt;a href="http://aspnetwebstack.codeplex.com/workitem/616" mce_href="http://aspnetwebstack.codeplex.com/workitem/616"&gt;bug&lt;/a&gt; in&amp;nbsp;ASP.NET MVC 4 and ASP.NET Web API. The issue is present in System.Web.PrefixContainer class which is used by both ASP.NET MVC and ASP.NET Web API assembly.&amp;nbsp;The details of this issue is available in this &lt;a href="http://forums.asp.net/t/1856357.aspx/1/10?FormValueProvider+skips+form+valuesI" mce_href="http://forums.asp.net/t/1856357.aspx/1/10?FormValueProvider+skips+form+valuesI"&gt;thread&lt;/a&gt;. This bug can be a breaking change for you if you upgraded your application to ASP.NET MVC 4 and your application model properties using the convention available in the above thread. So, I have created a package which will fix this issue both in ASP.NET MVC and ASP.NET Web API. In this article, I will show you how to use this package.&amp;nbsp;&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Create or open an ASP.NET MVC 4 project&amp;nbsp;and install &lt;a href="https://www.nuget.org/packages/ImranB.ModelBindingFix" mce_href="https://www.nuget.org/packages/ImranB.ModelBindingFix"&gt;ImranB.ModelBindingFix&lt;/a&gt; NuGet package.
Then, add this using statement on your global.asax.cs file,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;         using ImranB.ModelBindingFix;
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Then, just add this line in Application_Start method,&amp;nbsp;&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:csharp"&gt;	Fixer.FixModelBindingIssue();
	// For fixing only in MVC call this
	//Fixer.FixMvcModelBindingIssue();
	// For fixing only in Web API call this
	//Fixer.FixWebApiModelBindingIssue();
&lt;/pre&gt;
&lt;p mce_keep="true"&gt;. &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;This line will fix the model binding issue. If you are using Html.Action or&amp;nbsp;Html.RenderAction&amp;nbsp;then you should use Html.FixedAction or Html.FixedRenderAction instead to avoid this bug(make sure to&amp;nbsp;reference ImranB.ModelBindingFix.SystemWebMvc namespace). If you are using FormDataCollection.ReadAs extension method then you should use FormDataCollection.FixedReadAs instead to avoid this bug(make sure to reference ImranB.ModelBindingFix.SystemWebHttp namespace). The source code of this package is available&amp;nbsp;at &lt;a href="https://github.com/imranbaloch/ImranB.ModelBindingFix" mce_href="https://github.com/imranbaloch/ImranB.ModelBindingFix"&gt;github&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;There is a small but important issue/bug in ASP.NET MVC 4. In this article, I showed you how to fix this issue/bug by using a package.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9535665" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NEt+Web+API/default.aspx">ASP.NEt Web API</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Nuget/default.aspx">Nuget</category></item><item><title>Browser Specific Extensions of HttpClient</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/11/13/browser-specific-extensions-of-httpclient.aspx</link><pubDate>Tue, 13 Nov 2012 14:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9374238</guid><dc:creator>imran_ku07</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=9374238</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/11/13/browser-specific-extensions-of-httpclient.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; REpresentational State Transfer (REST) causing/leaving a great impact on service/API development because&amp;nbsp;it offers a way to access&amp;nbsp;a service without requiring any specific library&amp;nbsp;by embracing HTTP and its features. ASP.NET Web API&amp;nbsp;makes&amp;nbsp;it very easy to quickly build RESTful HTTP services. These HTTP services can be consumed by a variety of clients including browsers, devices, machines, etc.&amp;nbsp;With .NET Framework 4.5, we can use HttpClient class to consume/send/receive&amp;nbsp;RESTful HTTP services(for .NET Framework 4.0, HttpClient class&amp;nbsp;is shipped as part of ASP.NET Web API). The HttpClient&amp;nbsp;class provides a bunch of helper methods(for example, DeleteAsync, PostAsync, GetStringAsync, etc.) to&amp;nbsp;consume a HTTP&amp;nbsp;service very easily. ASP.NET Web API added some&amp;nbsp;more&amp;nbsp;extension methods(for example, PutAsJsonAsync, PutAsXmlAsync, etc) into HttpClient class to further simplify the usage. In addition, HttpClient is also an ideal choice for writing integration test for a RESTful HTTP service. Since a browser is a main client&amp;nbsp;of any RESTful API, it is also important to test&amp;nbsp;the HTTP service on a variety of browsers.&amp;nbsp;RESTful service&amp;nbsp;embraces HTTP&amp;nbsp;headers and different browsers send different HTTP headers. So, I have created a package that will add overloads(with an additional Browser parameter)&amp;nbsp;for almost all the helper methods of HttpClient class. In this article, I will show you how to use this package.&amp;nbsp;&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Create/open your&amp;nbsp;test project&amp;nbsp;and install &lt;a href="http://nuget.org/packages/ImranB.SystemNetHttp.HttpClientExtensions" mce_href="http://nuget.org/packages/ImranB.SystemNetHttp.HttpClientExtensions"&gt;ImranB.SystemNetHttp.HttpClientExtensions&lt;/a&gt; NuGet package.
Then, add this using statement on your class,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;         using ImranB.SystemNetHttp;
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Then, you can start using any HttpClient helper method which&amp;nbsp;include the additional Browser parameter. For example,&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:csharp"&gt;	var client = new HttpClient(myserver);
	var task = client.GetAsync("http://domain/myapi", Browser.Chrome);
	task.Wait();
	var response = task.Result;
&lt;/pre&gt;
&lt;p mce_keep="true"&gt;. &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Here is&amp;nbsp;the definition&amp;nbsp; of Browser, &lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:csharp"&gt;	
	public enum Browser
	{
	    Firefox = 0,
	    Chrome = 1,
	    IE10 = 2,
	    IE9 = 3,
	    IE8 = 4,
	    IE7 = 5,
	    IE6 = 6,
	    Safari = 7,
	    Opera = 8,
	    Maxthon = 9,
	}
&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;These extension methods will make it very easy&amp;nbsp;to write browser specific integration test.&amp;nbsp;It will also help&amp;nbsp;HTTP service consumer to mimic the request sending behavior&amp;nbsp;of&amp;nbsp;a browser.&amp;nbsp;This package source is available&amp;nbsp;on &lt;a href="https://github.com/imranbaloch/ImranB.SystemNetHttp.HttpClientExtensions" mce_href="https://github.com/imranbaloch/ImranB.SystemNetHttp.HttpClientExtensions"&gt;github&lt;/a&gt;.&amp;nbsp;So, you can grab the source and add some additional behavior on the top of these extensions.&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Testing&amp;nbsp;a REST&amp;nbsp;API is an important aspect of&amp;nbsp;service development and today, testing&amp;nbsp;with a browser is crucial.&amp;nbsp;In this article, I showed how to write integration test that will mimic the browser request sending behavior. I also showed&amp;nbsp;an example.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9374238" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NEt+Web+API/default.aspx">ASP.NEt Web API</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Nuget/default.aspx">Nuget</category></item><item><title>Advanced Options of ASP.NET Bundling and Minification</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/09/30/hidden-options-of-asp-net-bundling-and-minification.aspx</link><pubDate>Mon, 01 Oct 2012 03:54:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8785725</guid><dc:creator>imran_ku07</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8785725</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/09/30/hidden-options-of-asp-net-bundling-and-minification.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rick Anderson has written a great introduction&amp;nbsp;to ASP.NET Bundling and Minification at &lt;a href="http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification" mce_href="http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification"&gt;here&lt;/a&gt;.&amp;nbsp;In addition to the options presented by Rick, there are some other&amp;nbsp;features in System.Web.Optimization&amp;nbsp;framework which is also important for developers to understand in order to make&amp;nbsp;thier application performance and&amp;nbsp;architecture&amp;nbsp;good. In this article, I will show you some of the advance stuff of ASP.NET Bundling and Minification.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Server.MapPath is one of the most used method in ASP.NET.&amp;nbsp;Lot of libraries/assemblies&amp;nbsp;has used this&amp;nbsp;method. The problem of using this method inside a library &amp;nbsp;is that it will tie the users of this library with ASP.NET intrinsic Server object which makes unit testing&amp;nbsp;a little difficult. Fortunately,&amp;nbsp;System.Web.Optimization&amp;nbsp;have a special&amp;nbsp;property called &lt;strong&gt;BundleTable.MapPathMethod &lt;/strong&gt;which allows&amp;nbsp;users to provide a fake method for unit testing.
&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; There are some script files that you will never expect to add on a web page, for example, &lt;em&gt;*.intellisense.js&lt;/em&gt; and&amp;nbsp;&lt;em&gt;*-vsdoc.js&lt;/em&gt; script files which are used by Visual Studio for intellisense. There are some other script files that&amp;nbsp;you need to ignore during production, like, &lt;em&gt;*.debug.js &lt;/em&gt;file. There are some other script files that you will not add during debugging/testing, for e.g, &lt;em&gt;*.min.js &lt;/em&gt;and &lt;em&gt;*.min.css &lt;/em&gt;files. Luckily,&amp;nbsp;our optimization library will automatically takes care of these situation through &lt;strong&gt;BundleCollection.IgnoreList &lt;/strong&gt;property. Here is the what this property contains by default,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;	public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
	{
		if (ignoreList != null)
		{
			ignoreList.Ignore("*.intellisense.js");
			ignoreList.Ignore("*-vsdoc.js");
			ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
			ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
			ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
			return;
		}
		else
		{
			throw new ArgumentNullException("ignoreList");
		}
	}
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; You can easily override this by using &lt;strong&gt;BundleCollection.IgnoreList&lt;/strong&gt;&amp;nbsp;property in BundleConfig.cs file.&amp;nbsp;Note: it is also important that you should be aware about a &lt;a href="http://forums.asp.net/p/1834657/5158610.aspx/1?Re+Bundling+feature+in+MVC4" mce_href="http://forums.asp.net/p/1834657/5158610.aspx/1?Re+Bundling+feature+in+MVC4"&gt;bug&lt;/a&gt; in&amp;nbsp;the implementation&amp;nbsp;of this.&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;The ordering of scripts and css files is also very important. For example, if you have &lt;em&gt;jquery.js &lt;/em&gt;and &lt;em&gt;jquery.ui.js &lt;/em&gt;then&amp;nbsp;&lt;em&gt;jquery.js &lt;/em&gt;file should be appear before &lt;em&gt;jquery.ui.js&lt;/em&gt;. Similarly for css, &lt;em&gt;reset.css &lt;/em&gt;should appear first(if exist) and then &lt;em&gt;normalize.css&lt;/em&gt;(if exist) and then the remaining ones. Fortunately again,&amp;nbsp;our optimization assembly will also handle this scenario&amp;nbsp;by using&amp;nbsp;&lt;strong&gt;BundleCollection.FileSetOrderList &lt;/strong&gt;property. You can also override this inside BundleConfig.cs file. Here is the what this property contains by default,&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:csharp"&gt;	public static void AddDefaultFileOrderings(IList&amp;lt;bundlefilesetordering&amp;gt; list)
	{
		if (list != null)
		{
			BundleFileSetOrdering bundleFileSetOrdering = new BundleFileSetOrdering("css");
			bundleFileSetOrdering.Files.Add("reset.css");
			bundleFileSetOrdering.Files.Add("normalize.css");
			list.Add(bundleFileSetOrdering);
			BundleFileSetOrdering bundleFileSetOrdering1 = new BundleFileSetOrdering("jquery");
			bundleFileSetOrdering1.Files.Add("jquery.js");
			bundleFileSetOrdering1.Files.Add("jquery-min.js");
			bundleFileSetOrdering1.Files.Add("jquery-*");
			bundleFileSetOrdering1.Files.Add("jquery-ui*");
			bundleFileSetOrdering1.Files.Add("jquery.ui*");
			bundleFileSetOrdering1.Files.Add("jquery.unobtrusive*");
			bundleFileSetOrdering1.Files.Add("jquery.validate*");
			list.Add(bundleFileSetOrdering1);
			BundleFileSetOrdering bundleFileSetOrdering2 = new BundleFileSetOrdering("modernizr");
			bundleFileSetOrdering2.Files.Add("modernizr-*");
			list.Add(bundleFileSetOrdering2);
			BundleFileSetOrdering bundleFileSetOrdering3 = new BundleFileSetOrdering("dojo");
			bundleFileSetOrdering3.Files.Add("dojo.*");
			list.Add(bundleFileSetOrdering3);
			BundleFileSetOrdering bundleFileSetOrdering4 = new BundleFileSetOrdering("moo");
			bundleFileSetOrdering4.Files.Add("mootools-core*");
			bundleFileSetOrdering4.Files.Add("mootools-*");
			list.Add(bundleFileSetOrdering4);
			BundleFileSetOrdering bundleFileSetOrdering5 = new BundleFileSetOrdering("prototype");
			bundleFileSetOrdering5.Files.Add("prototype.js");
			bundleFileSetOrdering5.Files.Add("prototype-*");
			bundleFileSetOrdering5.Files.Add("scriptaculous-*");
			list.Add(bundleFileSetOrdering5);
			BundleFileSetOrdering bundleFileSetOrdering6 = new BundleFileSetOrdering("ext");
			bundleFileSetOrdering6.Files.Add("ext.js");
			bundleFileSetOrdering6.Files.Add("ext-*");
			list.Add(bundleFileSetOrdering6);
			return;
		}
		else
		{
			throw new ArgumentNullException("list");
		}
	}
&lt;/pre&gt;
&lt;p mce_keep="true"&gt;. &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now let's say that you have only registered the &lt;em&gt;jquery-1.6.2.js&lt;/em&gt;(not &lt;em&gt;jquery-1.6.2.min.js&lt;/em&gt;). The optimization framework will still&amp;nbsp;pick the&amp;nbsp;&lt;em&gt;jquery-1.6.2.min.js &lt;/em&gt;instead of &lt;em&gt;jquery-1.6.2.js &lt;/em&gt;during production.&amp;nbsp;This is possible via &lt;strong&gt;BundleCollection.FileExtensionReplacementList &lt;/strong&gt;property. Like others, you can also override this inside BundleConfig.cs file. Here is the what this property contains by default, &lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:csharp"&gt;	public static void AddDefaultFileExtensionReplacements(FileExtensionReplacementList list)
	{
		if (list != null)
		{
			list.Add("min", OptimizationMode.WhenEnabled);
			list.Add("debug", OptimizationMode.WhenDisabled);
			return;
		}
		else
		{
			throw new ArgumentNullException("list");
		}
	}
&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For clearing all these settings, you just need to call &lt;strong&gt;BundleCollection.ResetAll &lt;/strong&gt;method&lt;strong&gt;. &lt;/strong&gt;This method will clear all the above lists&amp;nbsp;and empty the registered bundles.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The System.Web.Optimization&amp;nbsp;framework also allows you to define a custom orderer for&amp;nbsp;a&amp;nbsp;bundle. You just need to implement the &lt;strong&gt;IBundleOrderer.OrderFiles&lt;/strong&gt; method in a new class and set the &lt;strong&gt;Bundle.Orderer&lt;/strong&gt; property.&amp;nbsp;Here is an &lt;a href="http://stackoverflow.com/questions/11979718/how-can-i-specify-an-explicit-scriptbundle-include-order" mce_href="http://stackoverflow.com/questions/11979718/how-can-i-specify-an-explicit-scriptbundle-include-order"&gt;example&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you some of the advanced options of System.Web.Optimization framework.&amp;nbsp;Hopefully you will enjoy my this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8785725" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Css/default.aspx">Css</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Bundling+and+Minification/default.aspx">Bundling and Minification</category></item><item><title>Display Mode Bug in ASP.NET MVC 4 and Workaround</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/08/31/display-mode-bug-in-asp-net-mvc-4-and-workaround.aspx</link><pubDate>Fri, 31 Aug 2012 04:26:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8898388</guid><dc:creator>imran_ku07</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8898388</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/08/31/display-mode-bug-in-asp-net-mvc-4-and-workaround.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I have found an important bug in ASP.NET &lt;a href="http://forums.asp.net/p/1824033/5066217.aspx/1?MVC+4+RC+Mobile+View+Cache+bug+" mce_href="http://forums.asp.net/p/1824033/5066217.aspx/1?MVC+4+RC+Mobile+View+Cache+bug+"&gt;forums&lt;/a&gt; regarding the new display mode feature in ASP.NET MVC 4. If you are using ASP.NET MVC 4 Display Mode feature in your application&amp;nbsp;or plan to use this feature in your application then it will be&amp;nbsp;very important for you&amp;nbsp;to be familiar with this bug, otherwise your application will not work as you expect. I and &lt;a href="http://blogs.msdn.com/b/rickandy/" mce_href="http://blogs.msdn.com/b/rickandy/"&gt;Rick Anderson&lt;/a&gt;&amp;nbsp;have created some&amp;nbsp;workaround for this bug.&amp;nbsp;So, in this article, I will explain you the bug and also let you know that how to workaround this bug in ASP.NET MVC 4.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For understanding this bug, create a new ASP.NET MVC 4&amp;nbsp;application. Then add a desktop and a mobile view(if you are new, then see this &lt;a href="http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features" mce_href="http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features"&gt;link&lt;/a&gt;).&amp;nbsp;Now, just&amp;nbsp;run your application and&amp;nbsp;switch&amp;nbsp;between the&amp;nbsp;mobile and the desktop view.&amp;nbsp;Then only use the desktop view for couple of minutes. Then switch&amp;nbsp;to the mobile view, you will see&amp;nbsp;the desktop view&amp;nbsp;instead of the mobile&amp;nbsp;view. This will clearly signal a bug in ASP.NET MVC 4. The reason of this bug is that&amp;nbsp;both of your desktop and mobile view's location are cached(assuming that you have only 2 display modes) when&amp;nbsp;your application access&amp;nbsp;a&amp;nbsp;view(desktop or mobile) first time.&amp;nbsp;Unfortunately,&amp;nbsp;the cache of view locations&amp;nbsp;is sliding. So, if your appliction uses destop view most of time, then your mobile view location will expire sooner than the&amp;nbsp;desktop view location.&amp;nbsp;So,&amp;nbsp;when your&amp;nbsp;application required the&amp;nbsp;mobile view, it will check the mobile view&amp;nbsp;location from cache. Since the mobile view location cache&amp;nbsp;is expired,&amp;nbsp;the application will not able to find this and&amp;nbsp;will use the desktop view location. See, &lt;a href="http://forums.asp.net/t/1824033.aspx/1/10?MVC+4+RC+Mobile+View+Cache+bug" mce_href="http://forums.asp.net/t/1824033.aspx/1/10?MVC+4+RC+Mobile+View+Cache+bug"&gt;this&lt;/a&gt; thread for more detail about this bug. You can easily work around this bug by using this code provided by Rick.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        // Code removed for clarity.
        // Cache never expires. You must restart application pool
        // when you add/delete a view. A non-expiring cache can lead to
        // heavy server memory load.
        ViewEngines.Engines.OfType&amp;lt;RazorViewEngine&amp;gt;().First().ViewLocationCache =
            new DefaultViewLocationCache(Cache.NoSlidingExpiration);
    
        // Add or Replace RazorViewEngine with WebFormViewEngine
        // if you are using the Web Forms View Engine.
    }
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This will&amp;nbsp;workaround this bug. But the problem with this workaround is that&amp;nbsp;the view&amp;nbsp;location in cache will never expire, so it will increase the load of your server. Another issue with this workaround is that you need to restart the application pool when you add or delete a view. I have created another workaround of this issue. The workaround simply&amp;nbsp;make all the view&amp;nbsp;location's time period&amp;nbsp;sync in cache. In&amp;nbsp;our example, if the mobile view location&amp;nbsp;in the cache is removed&amp;nbsp;due to cache time expiration then it will&amp;nbsp;remove the desktop view location from the cache as well. So,&amp;nbsp;all the view location time period remain sync in cache. Here is the work around,&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:csharp"&gt;    public class MyDefaultViewLocationCache : DefaultViewLocationCache, IViewLocationCache
    {
        public MyDefaultViewLocationCache(TimeSpan timeSpan): base(timeSpan)
        {
        }
        public MyDefaultViewLocationCache()
            : base()
        {
        }
        public new string GetViewLocation(HttpContextBase httpContext, string key)
        {
            var location = base.GetViewLocation(httpContext, key);
            if (location == null)
            {
                var cache = httpContext.Cache;
                RemoveAllCacheStartWith(key, cache);
            }
            return location;
        }
        private static void RemoveAllCacheStartWith(string key, System.Web.Caching.Cache cache)
        {
            var keyWithoutDisplayMode = key.Substring(0, key.Substring(0, key.Length - 1).LastIndexOf(':') + 1);
            var items = new List&amp;lt;string&amp;gt;();
            var enumerator = cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var _key = enumerator.Key.ToString();
                if (_key.StartsWith(keyWithoutDisplayMode))
                {
                    items.Add(_key);
                }
            }
            foreach (string item in items)
            {
                cache.Remove(item);
            }
        }
        public new void InsertViewLocation(HttpContextBase httpContext, string key, string virtualPath)
        {
            base.InsertViewLocation(httpContext, key, virtualPath);
        }
    }
&lt;/pre&gt;
&lt;br&gt;
&lt;br&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Just register this workaround inside&amp;nbsp;Application_Start event in global.asax file, &lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;    ViewEngines.Engines.OfType&amp;lt;RazorViewEngine&amp;gt;().First().ViewLocationCache =
                new MyDefaultViewLocationCache();
&lt;/pre&gt;
&lt;p&gt;
&lt;br&gt;
&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;u&gt;Update(15 Sep 2012)&lt;/u&gt;&lt;/strong&gt;:&amp;nbsp;Microsoft have published a &lt;a href="http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes" mce_href="http://nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes"&gt;workaround package&lt;/a&gt;&amp;nbsp;for this bug.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I&amp;nbsp;explained you a very important bug in ASP.NET MVC 4.&amp;nbsp;I have also showed you some work&amp;nbsp;around.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8898388" width="1" height="1"&gt;</description></item><item><title>Bundling and Minifying Inline Css and Js</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/07/25/bundling-and-minifying-inline-css-and-js.aspx</link><pubDate>Wed, 25 Jul 2012 17:40:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8782955</guid><dc:creator>imran_ku07</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8782955</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/07/25/bundling-and-minifying-inline-css-and-js.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application performance is the very important factor for an application success. Yahoo's &lt;a href="http://developer.yahoo.com/performance/rules.html/" mce_href="http://developer.yahoo.com/performance/rules.html/"&gt;Best Practices for Speeding Up Your Web Site&lt;/a&gt;&amp;nbsp;is a great resource for increasing your application performance. Out of these practices,&lt;em&gt; 'Putting Stylesheets at the Top'&lt;/em&gt;, &lt;em&gt;'Putting Scripts at the Bottom'&lt;/em&gt; and &lt;em&gt;'Minifying(external&amp;nbsp;and inline) JavaScript and CSS'&lt;/em&gt; are very important practices. Minifying inline css and js is also very important. From Yahoo Best Practices page &lt;em&gt;'In addition to minifying external scripts and styles, inlined &amp;lt;script&amp;gt; and &amp;lt;style&amp;gt; blocks can and should also be minified. Even if you gzip your scripts and styles, minifying them will still reduce the size by 5% or more. As the use and size of JavaScript and CSS increases, so will the savings gained by minifying your code &lt;/em&gt;'.&amp;nbsp;So, in this article, I will show you how to minify and bundle(combine all css/js)&amp;nbsp;your inline css/js.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Open your ASP.NET application(WebForm or MVC) and install &lt;a href="https://www.nuget.org/packages/BundleMinifyInlineJsCss" mce_href="https://www.nuget.org/packages/BundleMinifyInlineJsCss"&gt;BundleMinifyInlineJsCss&lt;/a&gt; nuget package.&lt;/p&gt;

&lt;br&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img style="width: 767px; height: 321px;" border="1" src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifyInstall.png" width="767" height="321" mce_src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifyInstall.png"&gt;&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Then register the response filter. If you are using WebForm, you can&amp;nbsp;register&amp;nbsp;response filter in a master page and if you are MVC, you can use register response filter in an action filter.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:csharp"&gt;    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Filter = new BundleAndMinifyResponseFilter(Response.Filter);
        }
    }
    public class BundleMinifyInlineCssJsAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Filter = new BundleAndMinifyResponseFilter(filterContext.HttpContext.Response.Filter);
        }
    }
    [BundleMinifyInlineCssJs]
    public class HomeController : Controller
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now just run your application. If a page view-source is, &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;img style="width: 689px; height: 503px;" border="1" src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifySource1.png" width="689" height="503" mce_src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifySource1.png"&gt;&lt;/p&gt;

&lt;br&gt;
&lt;br&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; After using the above response&amp;nbsp;filter, it will become,&lt;/p&gt;
&lt;br&gt;&lt;p mce_keep="true"&gt;&lt;img style="width: 688px; height: 230px;" border="1" src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifySource2.png" width="688" height="230" mce_src="http://weblogs.asp.net/blogs/imranbaloch/Bundling%20and%20Minifying%20Inline%20Css%20and%20Js/BundleMinifySource2.png"&gt;. &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Note in the above screen the&amp;nbsp;inline css moved to top, inline javascript moved to bottom and inline javascript/css is minified and bundled.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you how to you quickly and easily&amp;nbsp;put all your&amp;nbsp;inline css at the top, put all your js at bottom and minifying/bundle all your inline&amp;nbsp; javascript/css using a response filter.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8782955" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Css/default.aspx">Css</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Nuget/default.aspx">Nuget</category></item><item><title>Handling HTTP 404 Error in ASP.NET Web API</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/06/19/handling-http-404-error-in-asp-net-web-api.aspx</link><pubDate>Tue, 19 Jun 2012 19:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8619357</guid><dc:creator>imran_ku07</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8619357</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/06/19/handling-http-404-error-in-asp-net-web-api.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Building modern HTTP/RESTful/RPC services has become very easy with the new ASP.NET Web API framework. Using&amp;nbsp;ASP.NET Web API framework, you can create HTTP services which can be accessed from browsers, machines, mobile devices and other clients.&amp;nbsp;Developing&amp;nbsp;HTTP services is now become more easy for ASP.NET MVC developer becasue&amp;nbsp;ASP.NET Web API is now included in ASP.NET MVC.&amp;nbsp;In addition to developing HTTP services, it is also important to return meaningful response to client if a&amp;nbsp;resource(uri) not found(HTTP 404) for a reason(for example, mistyped resource uri). It is also important to make this response centralized so you can configure all of 'HTTP 404 Not Found'&amp;nbsp;resource at one place. In this article, I will show you how to&amp;nbsp;handle 'HTTP 404 Not Found' at one place. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Let's say that you are developing a HTTP RESTful application using ASP.NET Web API framework. In this application you need to handle HTTP 404 errors in a centralized location. From ASP.NET Web API point of you, you&amp;nbsp;need to handle these situations,&lt;/p&gt;
&lt;br&gt;

&lt;ul&gt;
&lt;li&gt;No route matched.&lt;/li&gt;

&lt;li&gt;Route is matched but no {controller} has been found on route.&lt;/li&gt;

&lt;li&gt;No type&amp;nbsp;with {controller} name has been&amp;nbsp;found.&lt;/li&gt;

&lt;li&gt;No matching action method found&amp;nbsp;in the&amp;nbsp;selected controller due to no action method start with&amp;nbsp;the request HTTP method&amp;nbsp;verb or no action method with IActionHttpMethodProviderRoute implemented attribute found or no&amp;nbsp;method with {action} name found or no method with the matching {action} name&amp;nbsp;found. &lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now, let create a ErrorController with Handle404 action method. This action method will be used in all of the above cases for sending HTTP 404 response message to the client.&amp;nbsp; &lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:c-sharp"&gt;    public class ErrorController : ApiController
    {
        [HttpGet, HttpPost, HttpPut, HttpDelete, HttpHead, HttpOptions, AcceptVerbs("PATCH")]
        public HttpResponseMessage Handle404()
        {
            var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
            responseMessage.ReasonPhrase = "The requested resource is not found";
            return responseMessage;
        }
    }
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; You can easily change the above action method to send some other specific HTTP 404 error response. If a client of your HTTP&amp;nbsp;service send a request to a resource(uri)&amp;nbsp;and no route matched with this uri on server then you can route the request to the above Handle404 method using&amp;nbsp;a custom route.&amp;nbsp;Put this route at the very&amp;nbsp;bottom of route configuration,&amp;nbsp;&lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:c-sharp"&gt;    routes.MapHttpRoute(
        name: "Error404",
        routeTemplate: "{*url}",
        defaults: new { controller = "Error", action = "Handle404" }
    );
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now you need handle the case when there is no {controller} in the matching route&amp;nbsp;or when there is  no type with {controller} name found.&amp;nbsp;You can easily handle this case and route the request to the above Handle404 method using a custom IHttpControllerSelector. Here is the definition&amp;nbsp;of a custom IHttpControllerSelector,&lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:c-sharp"&gt;    public class HttpNotFoundAwareDefaultHttpControllerSelector : DefaultHttpControllerSelector
    {
        public HttpNotFoundAwareDefaultHttpControllerSelector(HttpConfiguration configuration)
            : base(configuration)
        {
        }
        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            HttpControllerDescriptor decriptor = null;
            try
            {
                decriptor = base.SelectController(request);
            }
            catch (HttpResponseException ex)
            {
                var code = ex.Response.StatusCode;
                if (code != HttpStatusCode.NotFound)
                    throw;
                var routeValues = request.GetRouteData().Values;
                routeValues["controller"] = "Error";
                routeValues["action"] = "Handle404";
                decriptor = base.SelectController(request);
            }
            return decriptor;
        }
    }
&lt;/pre&gt;
&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next,&amp;nbsp;it is also&amp;nbsp;required to pass the request to the above Handle404 method if no matching action method found&amp;nbsp;in the selected controller due to the reason discussed above. This situation can also be easily handled through&amp;nbsp;a custom IHttpActionSelector. Here is the source of custom IHttpActionSelector,&amp;nbsp;&lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:c-sharp"&gt;    public class HttpNotFoundAwareControllerActionSelector : ApiControllerActionSelector
    {
        public HttpNotFoundAwareControllerActionSelector()
        {
        }

        public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            HttpActionDescriptor decriptor = null;
            try
            {
                decriptor = base.SelectAction(controllerContext);
            }
            catch (HttpResponseException ex)
            {
                var code = ex.Response.StatusCode;
                if (code != HttpStatusCode.NotFound &amp;amp;&amp;amp; code != HttpStatusCode.MethodNotAllowed)
                    throw;
                var routeData = controllerContext.RouteData;
                routeData.Values["action"] = "Handle404";
                IHttpController httpController = new ErrorController();
                controllerContext.Controller = httpController;
                controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "Error", httpController.GetType());
                decriptor = base.SelectAction(controllerContext);
            }
            return decriptor;
        }
    }
&lt;/pre&gt;
&lt;br&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Finally, we need to register the custom IHttpControllerSelector and IHttpActionSelector. Open global.asax.cs file and add these lines,&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;


&lt;pre class="brush:c-sharp"&gt;    configuration.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundAwareDefaultHttpControllerSelector(configuration));
    configuration.Services.Replace(typeof(IHttpActionSelector), new HttpNotFoundAwareControllerActionSelector());
&lt;/pre&gt;
&lt;br&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; In addition to building an application for HTTP services, it is also&amp;nbsp;important&amp;nbsp;to send meaningful&amp;nbsp;centralized information in response when something goes wrong, for example 'HTTP 404 Not Found' error. &amp;nbsp;In this article, I showed you how to handle 'HTTP 404 Not Found' error&amp;nbsp;in a centralized location.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8619357" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NEt+Web+API/default.aspx">ASP.NEt Web API</category></item><item><title>A jQuery Plugin for Google Map Thumb(Magnifier)</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/05/13/a-jquery-plugin-for-google-map-thumb-magnifier.aspx</link><pubDate>Sun, 13 May 2012 17:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8474663</guid><dc:creator>imran_ku07</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8474663</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/05/13/a-jquery-plugin-for-google-map-thumb-magnifier.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Recently in one of my project I came across with a requirement to show a thumbnail(or magnifier) in Google map because there was a lot of custom objects(line, polygon, etc) on my Google map due to which the map become obscure. I was able to successfully implement this option. But I have also created a jQuery plugin, so that if anyone else&amp;nbsp;came across the same&amp;nbsp;issue, can reuse the functionality. In this article, I will show you how to use this plugin. Here is a quick &lt;a href="http://66.85.130.174/jquerygmapthumb/GMapThumb.html" mce_href="http://66.85.130.174/jquerygmapthumb/GMapThumb.html"&gt;demo&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I am not going to bore you further, just download the jquery.gmapthumb sample&amp;nbsp;from &lt;a href="https://github.com/imranbaloch/jquery.gmapthumb" mce_href="https://github.com/imranbaloch/jquery.gmapthumb"&gt;github&lt;/a&gt;&amp;nbsp;or install it via &lt;a href="https://www.nuget.org/packages/jquery.gmapthumb/1.0.0" mce_href="https://www.nuget.org/packages/jquery.gmapthumb/1.0.0"&gt;nuget&lt;/a&gt;. Also,&amp;nbsp;a sample has been attached with this blog. You just need to include the jquery.gmapthumb.js(or jquery.gmapthumb.min.js)&amp;nbsp;file in your page and add this script. &lt;em&gt;$('#map').gmapthumb([map])&lt;/em&gt;. Note that&amp;nbsp;the map object is inside square bracket(array) because you can bind multiple maps with just one line(see example in above link for further reference). Finally, you need to place the img folder inside&amp;nbsp;your&amp;nbsp;page directory.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this article, I showed you how you can show a thumbnail(magnifier) in your Google map very quickly. I have also attached a sample with this blog.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8474663" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/imranbaloch/attachment/8474663.ashx" length="4873" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Google+Map/default.aspx">Google Map</category></item><item><title>Using Razor V2 in ASP.NET MVC 3</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/04/28/using-razor-v2-in-asp-net-mvc-3.aspx</link><pubDate>Sat, 28 Apr 2012 07:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8416152</guid><dc:creator>imran_ku07</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8416152</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/04/28/using-razor-v2-in-asp-net-mvc-3.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Razor view engine's popularity and usage is continuously increasing because it is very easy to learn and write. Also, it is very clean and easy to maintain. The beauty of Razor is that you can host it in any application not just in ASP.NET MVC or ASP.NET Web Pages. The latest version of Razor is 2 which is released with ASP.NET MVC 4 and ASP.NET Web Pages 2. Razor V2 includes a bunch of &lt;a href="http://vibrantcode.com/blog/2012/4/10/whats-new-in-razor-v2.html" mce_href="http://vibrantcode.com/blog/2012/4/10/whats-new-in-razor-v2.html"&gt;new&lt;/a&gt; &lt;a href="http://vibrantcode.com/blog/2012/4/13/what-else-is-new-in-razor-v2.html" mce_href="http://vibrantcode.com/blog/2012/4/13/what-else-is-new-in-razor-v2.html"&gt;features&lt;/a&gt;.&amp;nbsp;With the cool&amp;nbsp;new features of Razor, you may want to use these features&amp;nbsp;in your ASP.NET MVC 3 application.&amp;nbsp;In this article, I will show you how you can use Razor V2 in ASP.NET MVC 3 application.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For testing purpose, you can create a new ASP.NET MVC 3(Razor) application. From Solution Explorer, remove the reference of System.Web.WebPages 1.0 and System.Web.Helpers 1.0.&amp;nbsp;Then, add the reference of System.Web.WebPages 2.0 and System.Web.Helpers 2.0.&lt;/p&gt;

&lt;br&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;img style="width: 389px; height: 498px;" border="1" src="http://weblogs.asp.net/blogs/imranbaloch/UsingRazorV2inASP.NETMVC3/RemoveRefrence.png?p=1" width="389" height="498" mce_src="http://weblogs.asp.net/blogs/imranbaloch/UsingRazorV2inASP.NETMVC3/RemoveRefrence.png?p=1"&gt;&amp;nbsp;&lt;/p&gt;

&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inside root web.config file, add/update these settings,&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;      &amp;lt;appSettings&amp;gt;
            &amp;lt;add key="webpages:Version" value="2.0.0.0"/&amp;gt;
            &amp;lt;add key="webpages:Enabled" value="true" /&amp;gt;
            &amp;lt;add key="PreserveLoginUrl" value="true" /&amp;gt;
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      &amp;lt;add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&amp;gt;
      &amp;lt;add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&amp;gt;
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      &amp;lt;dependentAssembly&amp;gt;
            &amp;lt;assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /&amp;gt;
            &amp;lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&amp;gt;
      &amp;lt;/dependentAssembly&amp;gt;
      &amp;lt;dependentAssembly&amp;gt;
             &amp;lt;assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /&amp;gt;
	    &amp;lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&amp;gt;
      &amp;lt;/dependentAssembly&amp;gt;
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inside&amp;nbsp;Views web.config file, add/update these settings, &lt;/p&gt;

&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;      &amp;lt;configSections&amp;gt;
		&amp;lt;sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&amp;gt;
			&amp;lt;section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /&amp;gt;
			&amp;lt;section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /&amp;gt;
		&amp;lt;/sectionGroup&amp;gt;
      &amp;lt;/configSections&amp;gt;
      &amp;lt;system.web.webPages.razor&amp;gt;
		&amp;lt;host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&amp;gt;
&lt;/pre&gt;
&lt;br&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now you can easily use Razor V2 features in inside ASP.NET MVC 3 view. &lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:xhtml"&gt;      @{
            int? nullValue = null;
      }

      &amp;lt;a href="~/WebForm1.aspx" class="@nullValue"&amp;gt;MyLink&amp;lt;/a&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Note: Before doing this, make sure you have installed ASP.NET MVC 4. Otherwise the trick will fail.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; There is a bunch of improvement and new features in Razor V2.&amp;nbsp;In this article, I showed you how you can add Razor V2 inside ASP.NET MVC 3 application. I have also attached a sample ASP.NET MVC 3 application with above changes.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8416152" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/imranbaloch/attachment/8416152.ashx" length="143959" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Razor/default.aspx">Razor</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/WebMatrix/default.aspx">WebMatrix</category></item><item><title>Adding AjaxOnly Filter in ASP.NET Web API</title><link>http://weblogs.asp.net/imranbaloch/archive/2012/04/01/adding-ajaxonly-filter-in-asp-net-web-api.aspx</link><pubDate>Sun, 01 Apr 2012 11:45:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8372633</guid><dc:creator>imran_ku07</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/imranbaloch/rsscomments.aspx?PostID=8372633</wfw:commentRss><comments>http://weblogs.asp.net/imranbaloch/archive/2012/04/01/adding-ajaxonly-filter-in-asp-net-web-api.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Introduction:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Currently, ASP.NET MVC 4, ASP.NET Web API and ASP.NET Single Page Application&amp;nbsp;are the hottest topics in ASP.NET community. Specifically,&amp;nbsp;lot of developers loving&amp;nbsp;the inclusion of ASP.NET Web API in ASP.NET MVC.&amp;nbsp;ASP.NET Web API&amp;nbsp;makes it very simple to build HTTP RESTful services, which can be easily consumed from desktop/mobile browsers, silverlight/flash applications and&amp;nbsp;many different types of clients. Client side Ajax may be a very important consumer for various service providers. Sometimes,&amp;nbsp;some HTTP service providers&amp;nbsp;may need some(or all) of&amp;nbsp;thier services can only be accessed from Ajax. In this article, I will show you how to&amp;nbsp;implement AjaxOnly filter&amp;nbsp;in ASP.NET Web API application.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;u&gt;Description:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First of all you need to create a new ASP.NET MVC 4(Web API) application. Then, create a new AjaxOnly.cs file and add the following lines in this file,&lt;/p&gt;
&lt;br&gt;
&lt;pre class="brush:c-sharp"&gt;    public class AjaxOnlyAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var request = actionContext.Request;
            var headers = request.Headers;
            if (!headers.Contains("X-Requested-With") || headers.GetValues("X-Requested-With").FirstOrDefault() != "XMLHttpRequest")
                actionContext.Response = request.CreateResponse(HttpStatusCode.NotFound);
        }
    }
&lt;/pre&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This is an action filter which simply checks &lt;em&gt;X-Requested-With&lt;/em&gt; header in request with value &lt;em&gt;XMLHttpRequest&lt;/em&gt;. If &lt;em&gt;X-Requested-With&lt;/em&gt; header is not presant in request or this header value is not&amp;nbsp;&lt;em&gt;XMLHttpRequest&lt;/em&gt; then the filter will return 404(NotFound) response to the client.&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Now just&amp;nbsp;register this filter,&lt;/p&gt;
&lt;br&gt;

&lt;pre class="brush:c-sharp"&gt;        [AjaxOnly]
        public string GET(string input)
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; You can also register this filter globally, if your Web API application is only targeted for Ajax consumer.&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/b&gt;&lt;b&gt;&lt;u&gt;Summary:&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;
&lt;br&gt;
&lt;p mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ASP.NET WEB API provide a&amp;nbsp;framework for building RESTful&amp;nbsp;services. Sometimes, you may need your certain&amp;nbsp;API services can&amp;nbsp;only be accessed from Ajax. In this article, I showed you how to add AjaxOnly action filter in ASP.NET Web API.&amp;nbsp;Hopefully you will enjoy this article too. &lt;/p&gt;
&lt;br&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8372633" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/Ajax/default.aspx">Ajax</category><category domain="http://weblogs.asp.net/imranbaloch/archive/tags/ASP.NEt+Web+API/default.aspx">ASP.NEt Web API</category></item></channel></rss>