<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CE4ERXk8eCp7ImA9WhBbGUg.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800</id><updated>2013-05-19T05:41:44.770-03:00</updated><category term="web application" /><category term="XHQ" /><category term="logging" /><category term="Visual Studio" /><category term="text editor" /><category term="batch processing" /><category term="Haversine" /><category term="UI" /><category term="T4" /><category term="algorithms" /><category term="Apple" /><category term="upgrade" /><category term="Google Translator Toolkit" /><category term="ASP.NET" /><category term="CountIf" /><category term="Indie" /><category term="physical exercise" /><category term="self-improvement" /><category term="Google Spreadsheet API" /><category term="Apache" /><category term="letters" /><category term="code generation" /><category term="Parallels Desktop" /><category term="PUC-Rio" /><category term="SCM" /><category term="Compliments" /><category term="A*" /><category term="CSS" /><category term="IoC" /><category term="Web.config" /><category term="NHibernate Query Analyzer" /><category term="F#" /><category term="MSBuild" /><category term="IIS" /><category term="code snippet" /><category term="Firefox" /><category term="Excel Web App" /><category term="iTunes" /><category term="NuGet" /><category term="Transformations" /><category term="Dev-C++" /><category term="NHibernate" /><category term="MediaMonkey" /><category term="parameter" /><category term="svn" /><category term=".NET" /><category term="linked server" /><category term="answers" /><category term="technology" /><category term="computer virus" /><category term="priority queue" /><category term="management UI" /><category term="computer graphics" /><category term="Microsoft" /><category term="territory" /><category term="Finite State Machine" /><category term="list" /><category term="hooks" /><category term="Xcode" /><category term="cluster" /><category term="value frequency" /><category term="jQuery UI" /><category term="template" /><category term="tasks" /><category term="blob" /><category term="PowerShell" /><category term="Mac OS" /><category term="BSC" /><category term="image" /><category term="Applications" /><category term="productivity" /><category term="playlist" /><category term="update" /><category term="external connection" /><category term="public contest" /><category term="customization" /><category term="data binding" /><category term="paper" /><category term="flair" /><category term="web project" /><category term="MiniLyrics" /><category term="Lyricator" /><category term="counting" /><category term="PLINQ" /><category term="fortran" /><category term="compilers construction" /><category term="NPOI" /><category term="batch renaming" /><category term="port forwarding" /><category term="music" /><category term="size" /><category term="IIS 8" /><category term="oi" /><category term="monospaced" /><category term="Open Source" /><category term="properties" /><category term="queue" /><category term="PHP" /><category term="ExcelPackage" /><category term="data structures" /><category term="container" /><category term="ipod" /><category term="guess values" /><category term="Brazil" /><category term="source code" /><category term="Paparazzi" /><category term="ID3" /><category term="Datepicker" /><category term="fitness center" /><category term="academic" /><category term="health" /><category term="Google Data API" /><category term="continuous integration" /><category term="installation" /><category term="web" /><category term="encoding" /><category term="gadgets" /><category term="geocoder" /><category term="C" /><category term="generation y" /><category term="ASP.NET Chart" /><category term="Google Docs" /><category term="video game" /><category term="alignment" /><category term="SSMS" /><category term="Model-View-Presenter" /><category term="PIMS" /><category term="Git" /><category term="iPhoto" /><category term="LinkedIn" /><category term="function" /><category term="family" /><category term="IP" /><category term="UFRJ" /><category term="repair" /><category term="performance" /><category term="injection" /><category term="Chemtech" /><category term="NFA" /><category term="MPI" /><category term="OpenGL" /><category term="backup" /><category term="jQuery" /><category term="geotagging" /><category term="select" /><category term="CRM" /><category term="attribute" /><category term="retrospective" /><category term="tracking" /><category term="URL" /><category term="screen capture" /><category term="XML" /><category term="language" /><category term="regular expression" /><category term="user" /><category term="scan" /><category term="filter" /><category term="portlet" /><category term="Google Analytics" /><category term="Mac OS 10.6.7" /><category term="software" /><category term="widget" /><category term="IIS Express" /><category term="Google Maps" /><category term="MVC" /><category term="Portuguese" /><category term="WebGrid" /><category term="Date" /><category term="enterprise portal" /><category term="AJAX" /><category term="hidden field" /><category term="resource files" /><category term="photos" /><category term="RAM" /><category term="PlayStation" /><category term="code editor" /><category term="BBCode" /><category term="XLS" /><category term="SignalR" /><category term="download" /><category term="delete" /><category term="dynamic views" /><category term="data annotation" /><category term="script" /><category term="Get Lyrical" /><category term="file" /><category term="debug" /><category term="sharing" /><category term="quicksort" /><category term="resolver" /><category term="thread safety" /><category term="ERP" /><category term="games" /><category term="website" /><category term="Java" /><category term="font" /><category term="API" /><category term="Google App Engine" /><category term="textbox" /><category term="Snow Leopard" /><category term="C#" /><category term="GitHub" /><category term="upload" /><category term="Exif" /><category term="circular queue" /><category term="batch file" /><category term="metadata" /><category term="resize" /><category term="Win32 API" /><category term="tools" /><category term="package" /><category term="Castle Project" /><category term="development" /><category term="shopping" /><category term="robot" /><category term="XLSX" /><category term="Windows" /><category term="service" /><category term="lyrics" /><category term="modern age" /><category term="array" /><category term="webserver" /><category term="job" /><category term="configuration" /><category term="Hudson" /><category term="immutable data structures" /><category term="iOS" /><category term="IBM" /><category term="StackOverflow" /><category term="table" /><category term="prime number" /><category term="authentication" /><category term="bitwise operators" /><category term="data model" /><category term="intro" /><category term="Microsoft Excel" /><category term="ordinary differential equation" /><category term="HPC" /><category term="format" /><category term="memory" /><category term="postback" /><category term="server side" /><category term="webservice" /><category term="Tomcat" /><category term="iPhone" /><category term="text" /><category term="Magic mouse" /><category term="anniversary" /><category term="HttpContext" /><category term="Eclipse" /><category term="WinRAR" /><category term="project" /><category term="tree" /><category term="error" /><category term="query language" /><category term="critical path method" /><category term="reflection" /><category term="Foolproof" /><category term="coordinates" /><category term="English" /><category term="state transition diagram" /><category term="ProZ" /><category term="folder" /><category term="import" /><category term="locale" /><category term="input" /><category term="tag" /><category term="Oracle" /><category term="ResourceManager" /><category term="lifestyle" /><category term="DFA" /><category term="system analyst" /><category term="bulk" /><category term="partial class" /><category term="membership" /><category term="virtual machine" /><category term="learning" /><category term="artificial intelligence" /><category term="hardware" /><category term="repository" /><category term="helpers" /><category term="row" /><category term="embedded" /><category term="router" /><category term="photography" /><category term="Windsor" /><category term="highlight" /><category term="Localization" /><category term="gym" /><category term="MP3" /><category term="COM automation" /><category term="VisualSVN Server" /><category term="Babylon" /><category term="thread" /><category term="Google" /><category term="resx" /><category term="replace" /><category term="copyright" /><category term="IndieRockCafe" /><category term="RavenDB" /><category term="MES" /><category term="client server architecture" /><category term="battery life" /><category term="task" /><category term="computer engineering bachelor's degree" /><category term="RtPM" /><category term="server" /><category term="log4net" /><category term="OLPC" /><category term="Liferay" /><category term="numbers" /><category term="questions" /><category term="SQL" /><category term="architectural pattern" /><category term="management system" /><category term="buy" /><category term="quality of life" /><category term="time tracking" /><category term="column" /><category term="settings" /><category term="CPM" /><category term="validation" /><category term="digits" /><category term="connection string" /><category term="publish" /><category term="Apps" /><category term="society" /><category term="software engineering" /><category term="missing numbers" /><category term="shortcuts" /><category term="Mac" /><category term="link" /><category term="nanotechnology" /><category term="character set" /><category term="Last.fm" /><category term="alphabet" /><category term="LINQ" /><category term="Petrobras" /><category term="threads" /><category term="LIMS" /><category term="XSLT" /><category term="tips and tricks" /><category term="client side" /><category term="sequence" /><category term="badge" /><category term="DLL" /><category term="MVP" /><category term="Mp3tag" /><category term=".NET Framework" /><category term="Mac mini" /><category term="Blogger" /><category term="HQL" /><category term="thumbnail" /><category term="soapUI" /><category term="radio button" /><category term="inversion of control" /><category term="Automator" /><category term="Regex" /><category term="editor" /><category term="robot arm" /><category term="masters degree" /><category term="bar" /><category term="Excel table" /><category term="Mouse" /><category term="color" /><category term="time saver" /><category term="HTML" /><category term="Razor" /><category term="GPS" /><category term="dependency" /><category term="screenshot" /><category term="Right" /><category term="methods" /><category term="testing" /><category term="OOXML" /><category term="automation" /><category term="JavaScript" /><category term="TortoiseSVN" /><category term="entreprise management" /><category term="chemsite" /><category term="Excel chart" /><category term="navbar" /><category term="Windows 8" /><category term="elFinder.Net" /><category term="revision control" /><category term="asynchronous" /><category term="dynamic" /><category term="SQL Server" /><category term="Sublime Text" /><category term="manager" /><category term="export" /><category term="graph" /><category term="ASP.NET MVC" /><category term="extension methods" /><category term="string" /><category term="C++" /><category term="compression" /><category term="IndX" /><category term="distributed database" /><category term="spreadsheet" /><category term="Objective-C" /><category term="Siemens" /><category term="top-down approach" /><category term="internet" /><category term="job interview" /><category term="hide" /><category term="Rock" /><category term="crawler" /><category term="traversal" /><category term="database" /><category term="Windows 7" /><category term="L10n" /><category term="setTimeout" /><category term="parallel programming" /><category term="stored procedure" /><category term="translation" /><category term="wirelles keyboard" /><category term="programming" /><category term="first" /><category term="NoSQL" /><category term="binary search" /><category term="dictionary" /><category term="web deploy" /><category term="parser" /><category term="data" /><category term="password" /><category term="profile" /><title>Leniel Macaferi's blog</title><subtitle type="html">Everything software engineering with a strong background in computer engineering and computer science.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.leniel.net/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://www.leniel.net/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default?start-index=16&amp;max-results=15&amp;redirect=false&amp;v=2" /><author><name>Leniel Macaferi</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>144</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>15</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/leniel" /><feedburner:info uri="leniel" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" /><logo>http://creativecommons.org/images/public/somerights20.gif</logo><entry gd:etag="W/&quot;CE4ERXk8fSp7ImA9WhBbGUg.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-2916272568301001916</id><published>2013-05-19T01:50:00.000-03:00</published><updated>2013-05-19T05:41:44.775-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-19T05:41:44.775-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="performance" /><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="thread" /><category scheme="http://www.blogger.com/atom/ns#" term="queue" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="UI" /><category scheme="http://www.blogger.com/atom/ns#" term="batch processing" /><category scheme="http://www.blogger.com/atom/ns#" term="setTimeout" /><title>Prevent JavaScript code blocking the UI thread with setTimeout + a handy stopWatch to profile JS code</title><content type="html">&lt;p&gt;In early January of this year I was implementing some &lt;a href="http://en.wikipedia.org/wiki/JavaScript" target="_blank"&gt;JavaScript&lt;/a&gt; code that does a lot of processing on the client side with the help of &lt;a href="http://jquery.com/" target="_blank"&gt;jQuery&lt;/a&gt;. It’s part of a project where my customer asked for the possibility of answering a &lt;em&gt;questionnaire&lt;/em&gt; in two ways: using a &lt;code&gt;&lt;a href="http://afana.me/post/create-wizard-in-aspnet-mvc-3.aspx" target="_blank"&gt;Wizard&lt;/a&gt;&lt;/code&gt; (one question at a time) and &lt;code&gt;Listing&lt;/code&gt; (all questions at once) in a single form. The client side code for the Listing is the culprit and the subject of this post. It’s also related to this question I posted at StackOverflow on February 19:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stackoverflow.com/q/14961290/114029" target="_blank"&gt;Why ASP.NET MVC default Model Binder is slow? It's taking a long time to do its work&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;That question on SO is the inverse part, that is, the server receiving the client side data, but that’s just the other “part” of the problem which I also managed to solve. I really hope that this post helps you understand the problem with the client side part at least…&lt;/p&gt;  &lt;p&gt;One of the things that I didn’t like in that JavaScript code was that while the questions were being processed to be rendered subsequently by the JavaScript/jQuery client side code, the browser UI thread hung/froze a lot of times – I think it’s worth mentioning here that I didn’t get Firefox’s warning prompt for &lt;a href="https://support.mozilla.org/en-US/kb/warning-unresponsive-script" target="_blank"&gt;&amp;quot;Unresponsive script&amp;quot;&lt;/a&gt;. Anyway, the simple fact that the page freezes gives the user and me the developer a feeling that something is wrong with the code. As users we expect things to be fluid – we expect a good UI experience and seeing your browser freezing while loading a web page is not the best impression one may have of your software product.&lt;/p&gt;  &lt;p&gt;I’m more experienced with server side code and haven’t ever seen any of my JavaScript code run slow on the client. I then started searching on Google why this was happening and just stumbled on this really helpful and insightful article at O’Reilly Answers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://answers.oreilly.com/topic/1506-yielding-with-javascript-timers/" target="_blank"&gt;Yielding with JavaScript Timers&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Read it carefully to get a grasp of the concepts involved. I read it maybe thrice at least to understand the code and adapt it to the problem at hand. It fitted so well in my situation that the result I got after adapting the code was mind blowing.&lt;/p&gt;  &lt;p&gt;The part that really got my attention was the &lt;strong&gt;&lt;em&gt;Timed Code &lt;/em&gt;&lt;/strong&gt;section - way bellow that O’Reilly article. It tells us that batch processing items (questions in my case) instead processing everything once or one at a time is more efficient to avoid blocking the UI.&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="JavaScript UI Queue and UI Thread lanes depicted: timed code is intercalated taking turns" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="JavaScript UI Queue and UI Thread lanes depicted: timed code is intercalated taking turns" src="http://lh6.ggpht.com/-2rdzv4m_eBo/UZhab7-ytWI/AAAAAAAAC6s/rdRk3Ktgk0Q/JavaScriptUIThreadQueueDiagram_thumb%25255B3%25255D%25255B5%25255D.png?imgmax=800" width="600" height="297" /&gt;Figure 1 - JavaScript UI Queue and UI Thread lanes depicted: timed code is intercalated taking turns&lt;/p&gt;  &lt;p&gt;The JavaScript code I implemented processes a bunch of questions I receive from the server to prepare them to be shown to the user. Using a AJAX GET request’s success callback I dumped all the questions (usually 100 up to more than 200 sometimes and each one formatted with the help of a ASP.NET partial view) inside a div element &lt;font face="Courier New"&gt;&lt;font size="2"&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#questions&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)&lt;/span&gt;&lt;/font&gt;&lt;/font&gt; like this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: white; color: green"&gt;// Loading Questions for the Chapter selected...
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;loadQuestions(chapterId)
{
    $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#questions&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).fadeOut(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'slow'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).empty();
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;
    $.ajax({
        type: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        url: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Url.Action(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.UserAssessment.ActionNames.List, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.UserAssessment.Name)&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        data: { assessmentId: &lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Model.AssessmentId, chapterId : chapterId },
        cache: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        success: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(questions)
        {           
            $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#questions&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).html(questions);
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;
            setUpQuestions();
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;        },
        error: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function&lt;/span&gt;&lt;span style="background: white; color: black"&gt;()
        {
            alert(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.Raw(&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;.UnknownErrorAjax)&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);
        }
    });
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;With this I had all the questions’ HTML beautifully inserted on the page but I needed to process this HTML before showing it to the user. That’s where the &lt;font face="Courier New"&gt;setUpQuestions();&lt;/font&gt; method played its role. It does the heavy lifting and was where the whole thingy just got screwed up – the browser hung from time to time while inside that method… How did I discover that the problem was inside that method? I used &lt;a href="https://getfirebug.com/" target="_blank"&gt;Firebug&lt;/a&gt;’s Console and the JavaScript’s Date object as shown in that O’Reilly article. In &lt;font face="Courier New"&gt;setUpQuestions &lt;/font&gt;I use &lt;a href="http://api.jquery.com/find/" target="_blank"&gt;jQuery’s find&lt;/a&gt;, setup form fields validation with &lt;a href="https://github.com/jzaefferer/jquery-validation" target="_blank"&gt;jQuery validation plugin&lt;/a&gt;, disable/enable fields, apply CSS styles to the questions, etc and all of this was tackled all at once by the poor browser JavaScript engine.&lt;/p&gt;

&lt;p&gt;To profile &lt;font face="Courier New"&gt;setUpQuestions&lt;/font&gt; I created a &lt;font size="2" face="Courier New"&gt;stopWatch&lt;/font&gt; JavaScript method that receives &lt;font face="Courier New"&gt;setUpQuestions&lt;/font&gt; method as the &lt;font face="Courier New"&gt;func&lt;/font&gt; parameter:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;stopWatch(func)
{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;start = +&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Date(), stop;

    func();

    stop = +&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Date();&lt;br /&gt;
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(stop - start &amp;lt; 50)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="background: white; color: green"&gt;//alert(&amp;quot;Just about right.&amp;quot;);&lt;br /&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;code&gt;console.log(&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Just about right.&amp;quot;);&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;}
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;else&lt;br /&gt;    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;span style="background: white; color: green"&gt;        //alert(&amp;quot;Taking too long.&amp;quot;);&lt;br /&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;code&gt;console.log(“&lt;span style="background: white; color: #a31515"&gt;Taking too long.&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;    }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;According to that O’Reilly article, the author recommends never letting any Javascript code execute for longer than 50 milliseconds continuously, just to make sure the code never gets close to affecting the user experience – blocking the UI thread.&lt;/p&gt;

&lt;p&gt;When I ran the code selecting different chapters with varying number of questions I kept getting “Taking too long” and then I found where the problem was. I knew it was time to adapt the code presented in the &lt;strong&gt;&lt;em&gt;Timed Code&lt;/em&gt;&lt;/strong&gt; section of that &lt;a href="http://answers.oreilly.com/topic/1506-yielding-with-javascript-timers/" target="_blank"&gt;O’Reilly article&lt;/a&gt;. So here it is and commented where appropriate to make understanding it a little bit easier:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: green"&gt;/// More about it here: &lt;a href="http://answers.oreilly.com/topic/1506-yielding-with-javascript-timers/" target="_blank"&gt;http://answers.oreilly.com/topic/1506-yielding-with-javascript-timers/&lt;/a&gt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&lt;font color="#008000"&gt;&lt;br /&gt;&lt;/font&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;timedProcessArray(items, process, callback)
{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;todo = $.makeArray(items);

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// The first call to setTimeout() creates a timer to process the first item in the array.
    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;setTimeout(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;()
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;start = +&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Date();

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;do
        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
            &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Calling todo.shift() returns the first item and also removes it from the array.
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;process(todo.shift());

        } &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// After processing the item, a check is made to determine whether there are more items to process and if the time hasn't exceeded the threshold of 50 milliseconds
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;while &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(todo.length &amp;gt; 0 &amp;amp;&amp;amp; (+&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Date() - start &amp;lt; 50));

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(todo.length &amp;gt; 0)
        {
            &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Because the next timer needs to run the same code as the original, arguments.callee is passed in as the first argument.
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;setTimeout(arguments.callee, 25);
        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;else
        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{   &lt;/span&gt;&lt;span style="background: white; color: green"&gt;//If there are no further items to process, then a callback() function is called.
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(callback)
            {
                callback(items);
            }
        }

    }, 25);
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This code made the whole thing fluid and now the user has a much better experience while interacting with the page despite it having a lot of form controls. What it basically does is: process a batch of items/questions and then allows the UI thread to take some processing time and then it repeats until there’s no more questions left in the &lt;font face="Courier New"&gt;todo&lt;/font&gt; array. The &lt;font face="Courier New"&gt;process&lt;/font&gt; method is actually the &lt;font face="Courier New"&gt;setUpQuestions&lt;/font&gt; method that gets passed as a parameter called &lt;font face="Courier New"&gt;process&lt;/font&gt;.&lt;/p&gt;

&lt;p&gt;In my specific case, each question has 4 HTML input elements (input/text, select, etc). If the user selects a Manual’s Chapter to answer and this Chapter contains 230 questions for example, the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; element will contain about &lt;code&gt;920 controls = 4 x 230.&lt;/code&gt;That's a lot of controls to be processed by the JavaScript code inside the &lt;font face="Courier New"&gt;setUpQuestions &lt;/font&gt;method. Now no matter how many controls are present in the HTML code. &lt;font face="Courier New"&gt;timedProcessArray&lt;/font&gt; will handle this easily allowing the UI thread to breath from time to time.&lt;/p&gt;

&lt;p&gt;This is the modified version of the &lt;font face="Courier New"&gt;loadQuestions&lt;/font&gt; method that makes use of this life saving &lt;font face="Courier New"&gt;timedProcessArray&lt;/font&gt; method where &lt;a href="http://stackoverflow.com/tags/settimeout/info" target="_blank"&gt;setTimeout&lt;/a&gt; shines:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: green"&gt;// Loading Questions for the Chapter selected...
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;loadQuestions(chapterId)
{
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;    $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#questions&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).empty();

    $.ajax({
        type: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        url: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Url.Action(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.SAvE.UserAssessment.ActionNames.Answer, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.SAvE.UserAssessment.Name)&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        data: { assessmentId: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Model.AssessmentId&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, chapterId : chapterId, format: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Assessment&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Format&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.List&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;' &lt;/span&gt;&lt;span style="background: white; color: black"&gt;},
        cache: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        success: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(data)
        {
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;questions = $(data);

            questions.hide().appendTo(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#questions&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);

            &lt;/span&gt;&lt;span style="background: white; color: green"&gt;//stopWatch(function(){ return timedProcessArray(questions, setupQuestion, stats)});

            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;timedProcessArray(questions, setupQuestion);

&lt;pre class="code"&gt;&lt;span style="background: white; color: green"&gt;            //stats(questions);&lt;br /&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;},
        error: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function&lt;/span&gt;&lt;span style="background: white; color: black"&gt;()
        {
            alert(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.Raw(&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;.UnknownErrorAjax)&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);
        }
    });
}&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;I didn’t pass a callback function to &lt;font size="2" face="Courier New"&gt;timedProcessArray&lt;/font&gt; but that’s up to you.&lt;/p&gt;

&lt;p&gt;Hope it helps you take the most out of your highly intensive processing JavaScript code.&lt;/p&gt;

&lt;p&gt;As a last note, with the arrival of &lt;a href="http://en.wikipedia.org/wiki/HTML5" target="_blank"&gt;HTML5&lt;/a&gt; we now have &lt;a href="http://msdn.microsoft.com/en-us/hh549259.aspx" target="_blank"&gt;Web Workers&lt;/a&gt; but browser support is still &lt;a href="http://caniuse.com/#search=worker" target="_blank"&gt;limited&lt;/a&gt;. Things are getting better for us developers. &lt;img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Smile" src="http://lh6.ggpht.com/-0WadlEs9XPs/UZhaciQvn_I/AAAAAAAAC6k/90bIeAiHlJI/wlEmoticon-smile%25255B2%25255D.png?imgmax=800" /&gt; In the near future this will be standard for sure but till then we must find a way to solve the problem with the proven tools/code. &lt;font face="Courier New"&gt;setTimeout&lt;/font&gt; is one of them.&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Lpnvk_zYWms:AMY256UBGFE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Lpnvk_zYWms:AMY256UBGFE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Lpnvk_zYWms:AMY256UBGFE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Lpnvk_zYWms:AMY256UBGFE:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Lpnvk_zYWms:AMY256UBGFE:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Lpnvk_zYWms:AMY256UBGFE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/Lpnvk_zYWms" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/2916272568301001916?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/2916272568301001916?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/Lpnvk_zYWms/prevent-javascript-code-blocking-ui-thread-with-settimeout-batch-processing-items.html" title="Prevent JavaScript code blocking the UI thread with setTimeout + a handy stopWatch to profile JS code" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-2rdzv4m_eBo/UZhab7-ytWI/AAAAAAAAC6s/rdRk3Ktgk0Q/s72-c/JavaScriptUIThreadQueueDiagram_thumb%25255B3%25255D%25255B5%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2013/05/prevent-javascript-code-blocking-ui-thread-with-settimeout-batch-processing-items.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkMARXY-eip7ImA9WhBUEkw.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-7405777394718659294</id><published>2013-04-28T02:00:00.000-03:00</published><updated>2013-04-29T02:47:24.852-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-29T02:47:24.852-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="folder" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="manager" /><category scheme="http://www.blogger.com/atom/ns#" term="elFinder.Net" /><title>Manage folders &amp; files in your ASP.NET MVC app with elFinder.Net</title><content type="html">&lt;p&gt;Recently I had to evaluate what were my options when it comes to managing folders and files in an ASP.NET MVC project – a files manager somewhat like what a &lt;a href="https://en.wikipedia.org/wiki/Content_management_system" target="_blank"&gt;Content Management System&lt;/a&gt; does but I needed something way simpler and intuitive and principally of easy integration.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Story&lt;/strong&gt;     &lt;br /&gt;As part of a project requirement, a super user wants to able to offer other users some files like PDFs, Images, Videos, etc. This super user should be capable of creating a folder structure as he sees fit and then upload files to a central point (the server). All this looks like a difficult task to accomplish through a client server architecture since we’re dealing with clients and their files that need to go to the server. At first I thought about using a SharePoint module/&lt;a href="http://en.wikipedia.org/wiki/Web_part" target="_blank"&gt;web part&lt;/a&gt; but then it seemed overkill for what I wanted.&lt;/p&gt;  &lt;p&gt;As always I just put some simple words in a Google search and to my surprise I found an outstanding open source project called &lt;a href="https://github.com/Studio-42/elFinder#readme" target="_blank"&gt;elFinder by Studio 42&lt;/a&gt;. Here’s their &lt;a href="https://en.wikipedia.org/wiki/ASCII_art" target="_blank"&gt;ASCII art&lt;/a&gt; for your amusement:&lt;/p&gt;  &lt;div align="center"&gt;   &lt;pre&gt;      _ ______ _           _           
     | |  ____(_)         | |          
  ___| | |__   _ _ __   __| | ___ _ __ 
 / _ \ |  __| | | '_ \ / _` |/ _ \ '__|
|  __/ | |    | | | | | (_| |  __/ |   
 \___|_|_|    |_|_| |_|\__,_|\___|_|   &lt;/pre&gt;
&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;elFinder is an open-source file manager for web, written in &lt;strong&gt;JavaScript using jQuery UI&lt;/strong&gt;. Creation is inspired by simplicity and convenience of Finder program used in Mac OS X operating system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After going through the readme file for a moment, I thought: Oh my god, this is just what I need. The only drawback in my case was that the connector (server part) of elFinder is written in PHP and I’m developing an ASP.NET MVC app. Then let me go to Google once again and type &lt;em&gt;elFinder ASP NET&lt;/em&gt;. To my surprise there are some ports to the .NET arena but they seem to lack integration simplicity, that is, the dependencies these ports need just interfere with my current code as was the case of the &lt;a href="https://elfinderaspnet.codeplex.com/" target="_blank"&gt;elFinder ASP.NET Connector&lt;/a&gt; that uses &lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;Autofac&lt;/a&gt; as a dependency. I use &lt;a href="http://docs.castleproject.org/Default.aspx?Page=MainPage&amp;amp;NS=Windsor&amp;amp;AspxAutoDetectCookieSupport=1" target="_blank"&gt;Castle Windsor&lt;/a&gt; for the &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank"&gt;IoC&lt;/a&gt; part and then this was a no go. A more careful read at the original &lt;a href="https://github.com/Studio-42/elFinder/wiki" target="_blank"&gt;elFinder wiki&lt;/a&gt; let me know about different ports of this amazing project: &lt;a href="https://github.com/Studio-42/elFinder/wiki/3rd-party-connectors%2C-plugins%2C-modules" target="_blank"&gt;3rd party connectors, plugins, modules&lt;/a&gt;. This page pointed me to &lt;a href="https://elfinder.codeplex.com/" target="_blank"&gt;ElFinder.NET connector for elFinder 2.x&lt;/a&gt; hosted at CodePlex.&lt;/p&gt;

&lt;p&gt;I downloaded ElFinder.NET’s &lt;a href="https://elfinder.codeplex.com/SourceControl/BrowseLatest" target="_blank"&gt;source code&lt;/a&gt; that comes with a sample ASP.NET MVC app and played with it. When I saw the amount of features available [ &lt;a href="https://github.com/Studio-42/elFinder/wiki/Client-configuration-options" target="_blank"&gt;Client options&lt;/a&gt; + &lt;a href="https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options" target="_blank"&gt;Connector options&lt;/a&gt; ] I&lt;em&gt; almost cried&lt;/em&gt; in &lt;em&gt;excitement &lt;img class="wlEmoticon wlEmoticon-cryingface" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Crying face" src="http://lh4.ggpht.com/-_UJkwDsyffk/UXy14cpiknI/AAAAAAAACu4/JbWcMde_UzA/wlEmoticon-cryingface%25255B2%25255D.png?imgmax=800" /&gt;&lt;/em&gt;. ElFinder.NET is a very well written and organized port put together by Evgeny Noskov. Congrats to him and to Studio 42 from Russia for sharing the code with the community. It works great and so I started integrating it with my ASP.NET MVC 4 app.&lt;/p&gt;

&lt;p&gt;Along the way I hit some missing features that I saw are part of the original elFinder by Studio 42. They are &lt;a href="https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options#wiki-startPath" target="_blank"&gt;startPath&lt;/a&gt; and &lt;a href="https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options#wiki-uploadMaxSize" target="_blank"&gt;uploadMaxSize&lt;/a&gt;. I contacted Evgeny through the CodePlex contact form and asked him about the option to set a start path in the root directory. Thinking he wouldn’t even answer me, I decided to implement it myself looking at the original &lt;a href="https://github.com/Studio-42/elFinder/blob/2.x/php/elFinderVolumeDriver.class.php" target="_blank"&gt;elFinder codebase&lt;/a&gt;. Today I got an e-mail from Evgeny telling me that he just &lt;a href="https://elfinder.codeplex.com/SourceControl/changeset/32345" target="_blank"&gt;added the start path option&lt;/a&gt; to elFinder.Net. What a joy! Then I wanted to set a max upload size and I just saw that this option was also not present in elFinder.Net. Asked about it in &lt;a href="https://elfinder.codeplex.com/discussions/441737" target="_blank"&gt;this discussion&lt;/a&gt; and Evgeny promptly &lt;a href="https://elfinder.codeplex.com/SourceControl/changeset/32353" target="_blank"&gt;added it to the library&lt;/a&gt;. I even had no time to try to implement this one… he went even further and added a must have feature that I didn’t notice was missing: file download.&lt;/p&gt;

&lt;p&gt;So after all this amazing story of “&lt;a href="http://www.leniel.net/2010/07/stackoverflow-best-place-share-learn.html" target="_blank"&gt;is giving that you receive&lt;/a&gt;” I decided to spread the word and write a blog post about elFinder.net…&lt;/p&gt;

&lt;p&gt;It’s enough of background info. Let me show you one simple use case and how you can take advantage of such outstanding open source project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Use Case 
    &lt;br /&gt;&lt;/strong&gt;Let’s say you want to let one super user with read/write permissions create a folder structure inside a given root directory in the server and upload files there. Other users accessing the app will then be presented with a page that has links that point to the 1st level folders of that root directory. These users will be able to only read those folders/files uploaded by the super user. Note: this app has some kind of membership implemented with roles and permissions granted to users. I’ll omit this part in this sample code for simplicity sake.&lt;/p&gt;

&lt;p&gt;The super user has access to a screen (Files menu option) like the following one that contains elFinder files manager:&lt;/p&gt;

&lt;p align="center"&gt;&lt;img title="Figure 1 - elFinder.Net file manager UI styled with jQuery UI supporting any folder depth, file/folder upload/download, delete, rename, copy/cut/paste, preview, properties, drag and drop, etc." style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Figure 1 - elFinder.Net file manager UI styled with jQuery UI supporting any folder depth, file/folder upload/download, delete, rename, copy/cut/paste, preview, properties, drag and drop, etc." src="http://lh3.ggpht.com/-0fXzDSDHV0w/UXy16CjaaCI/AAAAAAAACvo/ZN-Oc2zskLk/elFinder.NetFileManager_thumb%25255B6%25255D%25255B5%25255D.png?imgmax=800" width="625" height="587" /&gt;Figure 1 - elFinder.Net file manager UI styled with jQuery UI supporting any folder depth, file/folder upload/download, delete, rename, copy/cut/paste, preview, properties, drag and drop, etc.&lt;/p&gt;

&lt;p&gt;Other users when logging in for example will have access to a page (Home menu option) listing the 1st level folders like this:&lt;/p&gt;

&lt;p align="center"&gt;&lt;img title="Figure 2 - 1st level folders links allow users to click on them and have the selected folder opened in elFinder’s file manager automagically" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Figure 2 - 1st level folders links allow users to click on them and have the selected folder opened in elFinder’s file manager automagically" src="http://lh4.ggpht.com/-3IFNHx9Q3No/UXy17cVWCjI/AAAAAAAACvs/_UeB9C5Y0Bg/elFinder.NetFileManager1stLevelFoldersListing_thumb%25255B8%25255D%25255B5%25255D.png?imgmax=800" width="435" height="612" /&gt;Figure 2 - 1st level folders links allow users to click on them and have the selected folder opened in elFinder’s file manager &lt;em&gt;automagically&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When clicking the folder name link, the user will be sent to elFinder’s file manager and the folder will be selected automatically showing its content to the user. The user without super powers then is allowed only to read the folder content and if they want they can even download the files to their machines. The possibilities are endless…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Code 
    &lt;br /&gt;&lt;/strong&gt;The full code is available at this GitHub repo: &lt;a title="https://github.com/leniel/elFinder.Net" href="https://github.com/leniel/elFinder.Net" target="_blank"&gt;https://github.com/leniel/elFinder.Net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll find comments throughout the code. Make sure you read them carefully.&lt;/p&gt;

&lt;p&gt;Let’s start defining two action methods in the &lt;font face="Courier New"&gt;HomeController.&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;HomeController &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Controller
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    [&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;GET&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)]
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Index()
    {
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DirectoryInfo &lt;/span&gt;&lt;span style="background: white; color: black"&gt;di = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DirectoryInfo&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Server.MapPath(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Files/MyFolder&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;));&lt;br /&gt;
&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Enumerating all 1st level directories of a given root folder (MyFolder in this case) and retrieving the folders names.&lt;/span&gt;
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;folders = di.GetDirectories().ToList().Select(d =&amp;gt; d.Name);

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;View(folders);
    }

    [&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;GET&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;FileManager/{subFolder?}&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)]
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Files(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;subFolder)
    {&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="background: white; color: green"&gt;        // FileViewModel contains the root MyFolder and the selected subfolder if any&lt;/span&gt;
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileViewModel &lt;/span&gt;&lt;span style="background: white; color: black"&gt;model = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileViewModel&lt;/span&gt;&lt;span style="background: white; color: black"&gt;() { Folder = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;MyFolder&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, SubFolder = subFolder };

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;View(model);
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;font face="Courier New"&gt;Index&lt;/font&gt; action method corresponding View has this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@model &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;IEnumerable&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt;

&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@{
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;    ViewBag.Title = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;}

&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Available 1st level Folders - clicking will navigate you to the File Manager setting the selected folder as elFinder's start path.

&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;ul&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;foreach &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;folder &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;in &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Model)
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;li&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;a &lt;/span&gt;&lt;span style="background: white; color: red"&gt;href&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Url.Action(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Home.ActionNames.Files, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Home.Name, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{ subFolder = folder })&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;folder&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;a&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt; &amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;li&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;}
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;ul&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;font face="Courier New"&gt;Files&lt;/font&gt; action method corresponding View has this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@model &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileViewModel

&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@{
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;    ViewBag.Title = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Files&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;}

@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.Partial(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MVC&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Shared.Views.FilesForm, Model)&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;font face="Courier New"&gt;FilesForm&lt;/font&gt; partial view has elFinder’s client side interesting pieces of code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@model &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileViewModel

&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@{
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;    ViewBag.Title = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Files&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;}

@*&lt;/span&gt;&lt;span style="background: white; color: black"&gt; B&lt;/span&gt;&lt;span style="background: white; color: #006400"&gt;undles with elFinder's CSS and JavaScript files configured in App_Start\BundleConfig.cs&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;*@
@&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Styles&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Render(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Content/elfinder&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)
&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Scripts&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Render(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Scripts/elfinder&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)

&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;script &lt;/span&gt;&lt;span style="background: white; color: red"&gt;type&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;()
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;myCommands = elFinder.prototype._options.commands;

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;disabled = [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'extract'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'archive'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'resize'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'help'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'select'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;]; &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Not yet implemented commands in ElFinder.Net

        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;$.each(disabled, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(i, cmd)
        {
            (idx = $.inArray(cmd, myCommands)) !== -1 &amp;amp;&amp;amp; myCommands.splice(idx, 1);
        });

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;selectedFile = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;null&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;options = {
            url: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'/connector'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// connector route defined in the project folder App_Start\RouteConfig.cs
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;customData : { folder : &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Model.Folder&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, subFolder: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Model.SubFolder&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;' &lt;/span&gt;&lt;span style="background: white; color: black"&gt;}, &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// customData passed in every request to the connector as query strings. These values are used in FileController's Index method.
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;rememberLastDir: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Prevent elFinder saving in the Browser LocalStorage the last visited directory
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;commands: myCommands,
            &lt;/span&gt;&lt;span style="background: white; color: green"&gt;//lang: 'pt_BR', // elFinder supports UI and messages localization. Check the folder Content\elfinder\js\i18n for all available languages. Be sure to include the corresponding .js file(s) in the JavaScript bundle.
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;uiOptions: { &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// UI buttons available to the user
                &lt;/span&gt;&lt;span style="background: white; color: black"&gt;toolbar: [
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'back'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'forward'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'reload'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'home'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'up'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'mkdir'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'mkfile'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'upload'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'open'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'download'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'info'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'quicklook'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'copy'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'cut'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'paste'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'rm'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'duplicate'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'rename'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'edit'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;],
                    [&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'view'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'sort'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;]
                ]
            },

            handlers: {
                select: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event, elfinderInstance) {

                    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event.data.selected.length == 1) {
                        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;item = $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#' &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ event.data.selected[0]);
                        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(!item.hasClass(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'directory'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)) {
                            selectedFile = event.data.selected[0];
                            $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#elfinder-selectFile'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).show();
                            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
                        }
                    }
                    $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#elfinder-selectFile'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).hide();
                    selectedFile = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;null&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
                }
            }
        };
        $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#elfinder'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).elfinder(options).elfinder(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'instance'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);

        $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'.elfinder-toolbar:first'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).append(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&amp;lt;div class=&amp;quot;ui-widget-content ui-corner-all elfinder-buttonset&amp;quot; id=&amp;quot;elfinder-selectFile&amp;quot; style=&amp;quot;display:none; float:right;&amp;quot;&amp;gt;'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;+
        &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'&amp;lt;div class=&amp;quot;ui-state-default elfinder-button&amp;quot; title=&amp;quot;Select&amp;quot; style=&amp;quot;width: 100px;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);
        $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#elfinder-selectFile'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).click(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;() {
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(selectedFile != &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;null&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)
                $.post(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'file/selectFile'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, { target: selectedFile }, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(response) {
                    alert(response);
                });
               
        });
    });
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;script&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;

&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;div &lt;/span&gt;&lt;span style="background: white; color: red"&gt;id&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;elfinder&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;div&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The last part is the &lt;font face="Courier New"&gt;FileController&lt;/font&gt; that gets called in every &lt;em&gt;connector&lt;/em&gt; request:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileController &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Controller
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Index(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;folder, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;subFolder)
    {
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileSystemDriver &lt;/span&gt;&lt;span style="background: white; color: black"&gt;driver = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileSystemDriver&lt;/span&gt;&lt;span style="background: white; color: black"&gt;();

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;root = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Root&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(
                &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DirectoryInfo&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Server.MapPath(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Files/&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ folder)),
                &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;http://&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ Request.Url.Authority + &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;/Files/&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ folder)
        {&lt;br /&gt;         &lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;span style="background: white; color: black"&gt;&lt;span style="background: white; color: green"&gt;// Sample using ASP.NET built in Membership functionality...
         // Only the super user can READ (download files) &amp;amp; WRITE (create folders/files/upload files).
         // Other users can only READ (download files)
         // IsReadOnly = !User.IsInRole(AccountController.SuperUser)&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;pre class="code"&gt;&lt;/span&gt;            IsReadOnly = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,&lt;br /&gt;            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Alias = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Files&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Beautiful name given to the root/home folder
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;MaxUploadSizeInKb = 500 &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Limit imposed to user uploaded file &amp;lt;= 500 KB
        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;};

        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Was a subfolder selected in Home Index page?
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(!&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.IsNullOrEmpty(subFolder))
        {
            root.StartPath = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DirectoryInfo&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Server.MapPath(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Files/&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ folder + &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;/&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ subFolder));
        }

        driver.AddRoot(root);

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;connector = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Connector&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(driver);

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;connector.Process(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;this&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.HttpContext.Request);
    }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;SelectFile(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;target)
    {
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileSystemDriver &lt;/span&gt;&lt;span style="background: white; color: black"&gt;driver = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FileSystemDriver&lt;/span&gt;&lt;span style="background: white; color: black"&gt;();

        driver.AddRoot(
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Root&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(
                &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DirectoryInfo&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Server.MapPath(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;~/Files&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)),
                &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;http://&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ Request.Url.Authority + &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;/Files&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;) { IsReadOnly = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false &lt;/span&gt;&lt;span style="background: white; color: black"&gt;});

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;connector = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Connector&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(driver);

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Json(connector.GetFileByHash(target).FullName);
    }

}&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Hope it helps.&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=_d-0of1aNRE:Dp1T5YoyB7k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=_d-0of1aNRE:Dp1T5YoyB7k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=_d-0of1aNRE:Dp1T5YoyB7k:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=_d-0of1aNRE:Dp1T5YoyB7k:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=_d-0of1aNRE:Dp1T5YoyB7k:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=_d-0of1aNRE:Dp1T5YoyB7k:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/_d-0of1aNRE" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7405777394718659294?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7405777394718659294?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/_d-0of1aNRE/manage-folders-and-files-in-your-aspnet-mvc-project-with-elfinder-net-file-manager.html" title="Manage folders &amp;amp; files in your ASP.NET MVC app with elFinder.Net" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-_UJkwDsyffk/UXy14cpiknI/AAAAAAAACu4/JbWcMde_UzA/s72-c/wlEmoticon-cryingface%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2013/04/manage-folders-and-files-in-your-aspnet-mvc-project-with-elfinder-net-file-manager.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQDRXk7fip7ImA9WhBRF00.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-1170618456686607865</id><published>2013-03-06T20:32:00.000-03:00</published><updated>2013-03-07T22:32:54.706-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-03-07T22:32:54.706-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="IP" /><category scheme="http://www.blogger.com/atom/ns#" term="router" /><category scheme="http://www.blogger.com/atom/ns#" term="IIS" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><category scheme="http://www.blogger.com/atom/ns#" term="website" /><category scheme="http://www.blogger.com/atom/ns#" term="port forwarding" /><category scheme="http://www.blogger.com/atom/ns#" term="IIS 8" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows 8" /><category scheme="http://www.blogger.com/atom/ns#" term="web" /><category scheme="http://www.blogger.com/atom/ns#" term="external connection" /><title>IIS 8 Web Site in Windows 8 externally accessible to the World</title><content type="html">&lt;p&gt;Today I needed to allow a per to access an application that’s under development. Instead of buying a &lt;a href="http://www.winhost.com/a/leniel_l_4" target="_blank"&gt;cheap ASP.NET hosting&lt;/a&gt; account I decided to host the app on my local &lt;a href="http://en.wikipedia.org/wiki/Internet_Information_Services" target="_blank"&gt;IIS 8 server&lt;/a&gt;. The process to get this configured and working was somewhat exciting since I learned new things along the way.&lt;/p&gt;  &lt;p&gt;I’ll show in this post the steps I followed to have this working as expected…&lt;/p&gt;  &lt;p&gt;My setup:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Windows 8 running inside a virtual machine in &lt;a href="http://www.leniel.net/2011/05/access-aspnet-app-from-win-7-vm-mac-os.html" target="_blank"&gt;Parallels 8&lt;/a&gt; with IP address &lt;font face="Courier New"&gt;192.168.1.107&lt;/font&gt;; &lt;/li&gt;    &lt;li&gt;ASP.NET MVC 4 app deployed in the Default Web Site in IIS 8 that is configured to accept incoming connections in the standard port &lt;font face="Courier New"&gt;80&lt;/font&gt;; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://support.linksys.com/en-us/support/gateways/WAG200G" target="_blank"&gt;Linksys WAG200G&lt;/a&gt; router/modem connected to the outside/external &lt;em&gt;World&lt;/em&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Steps to follow:    &lt;br /&gt;1 - Go to the router’s management interface. In my case it’s located in the address &lt;font face="Courier New"&gt;192.168.1.1&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;2 - You’ll need to set a port forwarding configuration. In my case it’s located in Applications &amp;amp; Gaming / Single Port Forwarding.&lt;em&gt; It’ll vary slightly depending on your router vendor and model&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;Application: HTTP      &lt;br /&gt;External Port: 80       &lt;br /&gt;Internal Port: 80       &lt;br /&gt;Protocol: TCP       &lt;br /&gt;IP Address: 192.168.1.107 (your Windows/IIS machine IP address)       &lt;br /&gt;Enabled: True (checked)&lt;/font&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Linksys WAG200G Single Port Forwarding configuration" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Linksys WAG200G Single Port Forwarding configuration" src="http://lh4.ggpht.com/-v1zXebZa3vU/UTfTLCDIIjI/AAAAAAAAClM/nU0set5BvTs/LinksysWAG200GSingPortForwarding_thumb%25255B4%25255D%25255B5%25255D.png?imgmax=800" width="625" height="283" /&gt;Figure 1 - Linksys WAG200G Single Port Forwarding configuration&lt;/p&gt;  &lt;p&gt;Make sure to hit the &lt;em&gt;Save Settings&lt;/em&gt; button way bellow the page.&lt;/p&gt;  &lt;p&gt;3 - Go to Windows 8 Control Panel / System and Security / Windows Firewall / Turn Windows Firewall on or off.    &lt;br /&gt;Turn off Windows Firewall for Private Networks.&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Turning off Windows 8 Firewall for Private Networks" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Turning off Windows 8 Firewall for Private Networks" src="http://lh4.ggpht.com/-G-4KA3Xj_JU/UTfTMannWgI/AAAAAAAAClQ/9EBi4zRF7KI/TurningOffWindows8FirewallForPrivateNetworks_thumb%25255B5%25255D%25255B5%25255D.png?imgmax=800" width="625" height="459" /&gt;Figure 2 - Turning off Windows 8 Firewall for Private Networks&lt;/p&gt;  &lt;p&gt;4 - Take note of your internet IP address. You can see it in the router’s status page. In my case it’s located in Status / Gateway. &lt;em&gt;Again where you’ll find this info will vary depending on your router vendor&lt;/em&gt;.&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Taking note of the internet/Gateway IP that uniquely identifies the machine on the internet" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Taking note of the internet/Gateway IP that uniquely identifies the machine on the internet" src="http://lh3.ggpht.com/-299usIxcfkk/UTfTN3fUU3I/AAAAAAAAClU/zl3LtPHJfpk/LinksysWAG200GStatusGatewayIpAddress_thumb%25255B7%25255D%25255B5%25255D.png?imgmax=800" width="625" height="410" /&gt;Figure 3 - Taking note of the internet/Gateway IP that uniquely identifies the machine on the internet&lt;/p&gt;  &lt;p&gt;You can also see your current IP Address using &lt;a href="http://support.google.com/mail/bin/answer.py?hl=en&amp;amp;answer=45938&amp;amp;ctx=gmail" target="_blank"&gt;Gmail’s Last Account Activity report&lt;/a&gt; if you happen to have a Gmail account of course.&lt;/p&gt;  &lt;p&gt;5 - Open a browser window and type your internet Gateway IP address taken in step 4. You should be presented with the beautiful IIS 8 default web page if you have no app deployed in the Default Web Site; otherwise you should see your app’s default page/login view.&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="IIS 8 Default Web Site page" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="IIS 8 Default Web Site page" src="http://lh3.ggpht.com/-6IVxEZnnSQw/UTfTQ-cE94I/AAAAAAAAClc/dWKkjE_EQf4/IIS8DefaultWebPage_thumb%25255B4%25255D%25255B5%25255D.png?imgmax=800" width="625" height="514" /&gt;Figure 4 - IIS 8 Default Web Site page&lt;/p&gt;  &lt;p&gt;That’s it! Now the web site/app is available externally to any user in any part of the WORLD directly from my development machine.&lt;/p&gt;  &lt;p&gt;Anytime I want I can hit Publish from within Visual Studio 2012 and deploy directly to the local IIS 8 server. The user that knows my IP address can then see my ongoing work remotely and free of charge for now.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note 1      &lt;br /&gt;&lt;/strong&gt;I do not have a static IP address, that is, my &lt;a href="http://en.wikipedia.org/wiki/Internet_service_provider" target="_blank"&gt;ISP&lt;/a&gt; here in Brazil (&lt;a href="http://oivelox.oi.com.br/" target="_blank"&gt;Oi Velox&lt;/a&gt;) gives me a &lt;a href="http://en.wikipedia.org/wiki/IP_address#IP_address_assignment" target="_blank"&gt;dynamic IP address&lt;/a&gt;. This means that if there’s a power outage or if the router resets for whatever reason I’ll get a different address – it’s really important to know about this. To overcome such limitation there are some services that can help. One of them is &lt;a href="https://www.noip.com/" target="_blank"&gt;No-IP&lt;/a&gt;. It basically allows you to access your computer by a hostname instead of an IP address by using &lt;a href="http://en.wikipedia.org/wiki/Dynamic_DNS" target="_blank"&gt;dynamic DNS&lt;/a&gt;. They have a &lt;a href="http://www.noip.com/downloads.php" target="_blank"&gt;free utility app&lt;/a&gt; that runs in the background and that automatically syncs your current dynamic IP address with your custom hostname defined in No-IP service.&lt;/p&gt;  &lt;p&gt;Excerpts from No-IP site:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;What is a hostname?&lt;/strong&gt;       &lt;br /&gt;A hostname is a name given to a computer to make connecting to it easier. Instead of typing out a long IP address you can enter the hostname followed by the domain name, such as myhostname.no-ip.com.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;What is dynamic DNS?&lt;/strong&gt;       &lt;br /&gt;Dynamic DNS makes it possible to connect to computers with dynamic IP addresses without needing to know the actual IP address.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;They have a &lt;a href="http://www.noip.com/select-account-type.php" target="_blank"&gt;free account&lt;/a&gt; available with some limitations but it’s worth trying anyway.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note 2&lt;/strong&gt;     &lt;br /&gt;If for some reason your ISP blocks the default IIS port 80 (something common – hooray! not in my case today) you can try forwarding from a different external port like 8888 in step 2 above. In this case, you’d have to specify this port address when trying to access your IIS server. For example:&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;189.xxx.xxx.xxx:8888&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;where &lt;font size="2" face="Courier New"&gt;189.xxx.xxx.xxx &lt;/font&gt;is your internet IP address.&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6OvwmOC6TdM:bypp42PoDb4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6OvwmOC6TdM:bypp42PoDb4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6OvwmOC6TdM:bypp42PoDb4:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6OvwmOC6TdM:bypp42PoDb4:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6OvwmOC6TdM:bypp42PoDb4:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6OvwmOC6TdM:bypp42PoDb4:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/6OvwmOC6TdM" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1170618456686607865?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1170618456686607865?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/6OvwmOC6TdM/configuring-iis-8-website-app-in-windows-8-to-be-accessible-to-the-world-using-port-forwarding-linksys-wag200g-router.html" title="IIS 8 Web Site in Windows 8 externally accessible to the World" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-v1zXebZa3vU/UTfTLCDIIjI/AAAAAAAAClM/nU0set5BvTs/s72-c/LinksysWAG200GSingPortForwarding_thumb%25255B4%25255D%25255B5%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2013/03/configuring-iis-8-website-app-in-windows-8-to-be-accessible-to-the-world-using-port-forwarding-linksys-wag200g-router.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUMCQXw-fyp7ImA9WhBTF04.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-1044420679632971731</id><published>2013-02-13T01:55:00.000-02:00</published><updated>2013-02-13T03:31:00.257-02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-13T03:31:00.257-02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="array" /><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><category scheme="http://www.blogger.com/atom/ns#" term="data binding" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="parameter" /><category scheme="http://www.blogger.com/atom/ns#" term="postback" /><title>Data binding view values to an array parameter in ASP.NET MVC</title><content type="html">&lt;p align="center"&gt;If you want the source codez, jump &lt;a href="http://www.leniel.net/2013/02/data-binding-view-values-to-an-array-type-or-viewmodel-property-in-aspnet-mvc.html#SourceCode"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;This looks like something so easy to be done and at the same time I guess a lot of people fail the first time they try to get this working. This &lt;a href="http://stackoverflow.com/q/14737180/114029" target="_blank"&gt;question&lt;/a&gt; at StackOverflow proves that. So to help the fellow developers out there I put together a concise ASP.NET MVC 4 sample application to show how to data bind/postback view values to an array parameter in an controller action method. I show two ways of doing this: using a traditional/full postback and an AJAX/partial postback. This way you can choose the best option for you.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Use case      &lt;br /&gt;&lt;/strong&gt;&lt;em&gt;User wants to be able to select 3 values ( at least 1 being mandatory &lt;img class="wlEmoticon wlEmoticon-surprisedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Surprised smile" src="http://lh6.ggpht.com/-MVMin1tVEOE/URsYW4W_c-I/AAAAAAAACik/uhPcCwG59oU/wlEmoticon-surprisedsmile%25255B2%25255D.png?imgmax=800" /&gt; ) in the view using dropdowns and data bind the selected value(s) to &lt;font color="#ff0000"&gt;&lt;strong&gt;an array&lt;/strong&gt; &lt;/font&gt;that will be treated inside the controller.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;User Interface      &lt;br /&gt;&lt;/strong&gt;While implementing the code I thought why not make the app a little bit fancier and give it some meaning and so I complemented the sample with color picker dropdowns. This is the UI:&lt;/p&gt;  &lt;p&gt;&lt;img title="ASP.NET MVC Array Data Binding sample app UI" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; border-left: 0px; display: block; padding-right: 0px; margin-right: auto" border="0" alt="ASP.NET MVC Array Data Binding sample app UI" src="http://lh4.ggpht.com/-noCpTi9gKDQ/URsYX8v54ZI/AAAAAAAACjI/2W6hbsj67SQ/AspNetMvcArrayDataBindingSampleAppUI_thumb%25255B9%25255D%25255B5%25255D.png?imgmax=800" width="523" height="320" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;     &lt;br /&gt;ASP.NET MVC needs the generated form elements to follow a certain format so that the model binder can do its job, that is, understand what you’re trying to do; otherwise when you debug the code you’ll just get a &lt;font size="2" face="Courier New"&gt;null&lt;/font&gt; value in the array parameter or ViewModel array property inside you controller action. This means that the model binder didn’t understand your posted data and I’m sure you thought it would. I’ve already been in this situation…&lt;/p&gt;  &lt;p&gt;So the question is: How to make the model binder understand the view data? Let’s get to the code…&lt;/p&gt;  &lt;p&gt;The sample’s code is well documented, so I’ll just copy/paste the important parts here. Just read the comments to understand what’s going on…&lt;/p&gt;  &lt;p&gt;In the view side, you must create the input elements following this format:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@*&lt;/span&gt;&lt;span style="background: white; color: #006400"&gt; Pay special attention to how we must name the form fields.&lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: #006400"&gt;selectedColors is the name of a parameter defined in the controller action that will receive the form values. It’s necessary to suffix the name with [n], where n must be zero-based and unbroken. &lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;*@
@&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;for &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;int &lt;/span&gt;&lt;span style="background: white; color: black"&gt;i = 0; i &amp;lt; 3; i++)
{
    &lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.DropDownList(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;selectedColors[&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;+i+&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;]&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, Model.Colors, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;-- Pick a Color --&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In the controller side, you must have an action method with an array parameter named exactly like the name given to the dropdowns on the view side ( &lt;font size="2" face="Courier New"&gt;selectedColors&lt;/font&gt; ) like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="background: white; color: green"&gt;This action method works with a simple int[] array that's not part of a ViewModel.
&lt;/span&gt;&lt;span style="background: white; color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name=&amp;quot;selectedColors&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: white; color: green"&gt;Simple int[] array&lt;/span&gt;&lt;span style="background: white; color: gray"&gt;&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="background: white; color: green"&gt;ArrayPostResult view&lt;/span&gt;&lt;span style="background: white; color: gray"&gt;&amp;lt;/returns&amp;gt;
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;HttpPost&lt;/span&gt;&lt;span style="background: white; color: black"&gt;]
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;TestArrayPost(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;int&lt;/span&gt;&lt;span style="background: white; color: black"&gt;[] selectedColors)
{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Request.IsAjaxRequest())
    {
        ViewBag.PostBack = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;AJAX&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;PartialView(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;ArrayPostResult&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, selectedColors);
    }

    ViewBag.PostBack = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Full&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;View(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;ArrayPostResult&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, selectedColors);
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The above code should take care of handling the full postback scenario.&lt;/p&gt;

&lt;p&gt;To do an async/partial postback using AJAX you can use this jQuery code as an example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#submit-ajax&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).click(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;() {

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;selectedColors = [];

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// For each select HTML element/dropdown,
    // push its selected value into selectedColors
    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'select option:selected'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).each(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;() {
        selectedColors.push($(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;this&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).val());
    });

    $.ajax({
        type: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        url: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;/Home/TestArrayPost&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        dataType: &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        traditional: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;true&lt;/span&gt;&lt;span style="background: white; color: black"&gt;,
        data: { selectedColors: selectedColors },
        success: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(response) {

            $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#result&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).html(response).fadeIn();

            &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Hiding the GO back button...
            &lt;/span&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#result&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).find(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#go-back&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).hide();

        },
        error: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(xhr, textStatus, exceptionThrown) {
            $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#result'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).html(exceptionThrown);
        }
    });
});&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note 
    &lt;br /&gt;&lt;/strong&gt;If you want to postback the values to an array property that’s inside your &lt;font size="2" face="Courier New"&gt;ViewModel&lt;/font&gt;, for example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public int&lt;/span&gt;&lt;span style="background: white; color: black"&gt;[] SelectedColors { &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;get&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;set&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; }&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;You can do this in the view side:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;for &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;int &lt;/span&gt;&lt;span style="background: white; color: black"&gt;i = 0; i &amp;lt; 3; i++)
{
    &lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.DropDownListFor(m =&amp;gt; m.SelectedColors[i], Model.Colors, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;-- Pick a Color --&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;} &lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Then on the controller side, you’d have this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;HttpPost&lt;/span&gt;&lt;span style="background: white; color: black"&gt;]
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;TestArrayPost(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ColorsModel &lt;/span&gt;&lt;span style="background: white; color: black"&gt;model)
{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Request.IsAjaxRequest())
    {
        ViewBag.PostBack = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;AJAX&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;PartialView(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;ArrayPostResult&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, model.SelectedColors);
    }

    ViewBag.PostBack = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Full&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;View(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;ArrayPostResult&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, model.SelectedColors);
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;&lt;a name="SourceCode"&gt;Source code&lt;/a&gt; 

    &lt;br /&gt;&lt;/strong&gt;The code uses &lt;a href="https://github.com/leniel/AspNetMvcArrayDataBinding/blob/master/AspNetMvcArrayDataBinding/Scripts/AspnetMvcArrayDataBinding.js" target="_blank"&gt;a bit of jQuery&lt;/a&gt; to control the UI logic and to postback the values through AJAX. &lt;/p&gt;

&lt;p&gt;It’s hosted at GitHub and so you can navigate through it easily &lt;a href="https://github.com/leniel/AspNetMvcArrayDataBinding" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can download the Visual Studio 2012 solution here:&lt;/p&gt;

&lt;p&gt;&lt;a title="https://github.com/leniel/AspNetMvcArrayDataBinding/archive/master.zip" href="https://github.com/leniel/AspNetMvcArrayDataBinding/archive/master.zip"&gt;https://github.com/leniel/AspNetMvcArrayDataBinding/archive/master.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope it helps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References 
    &lt;br /&gt;&lt;/strong&gt;To implement this sample app I Googled some of the concepts employed like setting the &lt;font size="2" face="Courier New"&gt;background-color&lt;/font&gt; of select/dropdown options based on the option’s text. Pretty interesting stuff…&lt;/p&gt;

&lt;p&gt;The following is a list of links that helped me:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" target="_blank"&gt;Model Binding To A List&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx" target="_blank"&gt;ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://odetocode.com/blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx" target="_blank"&gt;Drop-down Lists and ASP.NET MVC&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://stackoverflow.com/q/14170444/114029" target="_blank"&gt;@Html.DropDownListFor; How to set different background color for each item in DDL?&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://stackoverflow.com/q/1167361/114029" target="_blank"&gt;How do I convert an enum to a list in C#?&lt;/a&gt;&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=OMxd2uum8B0:oDPNSGtPQ98:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=OMxd2uum8B0:oDPNSGtPQ98:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=OMxd2uum8B0:oDPNSGtPQ98:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=OMxd2uum8B0:oDPNSGtPQ98:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=OMxd2uum8B0:oDPNSGtPQ98:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=OMxd2uum8B0:oDPNSGtPQ98:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/OMxd2uum8B0" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1044420679632971731?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1044420679632971731?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/OMxd2uum8B0/data-binding-view-values-to-an-array-type-or-viewmodel-property-in-aspnet-mvc.html" title="Data binding view values to an array parameter in ASP.NET MVC" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-MVMin1tVEOE/URsYW4W_c-I/AAAAAAAACik/uhPcCwG59oU/s72-c/wlEmoticon-surprisedsmile%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2013/02/data-binding-view-values-to-an-array-type-or-viewmodel-property-in-aspnet-mvc.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkINR3s8cCp7ImA9WhNUEkw.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-7446231153786528716</id><published>2013-01-02T21:11:00.000-02:00</published><updated>2013-01-03T10:03:16.578-02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-01-03T10:03:16.578-02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="HttpContext" /><category scheme="http://www.blogger.com/atom/ns#" term="dependency" /><category scheme="http://www.blogger.com/atom/ns#" term="lifestyle" /><category scheme="http://www.blogger.com/atom/ns#" term="injection" /><category scheme="http://www.blogger.com/atom/ns#" term="Castle Project" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="Windsor" /><category scheme="http://www.blogger.com/atom/ns#" term="SignalR" /><category scheme="http://www.blogger.com/atom/ns#" term="task" /><category scheme="http://www.blogger.com/atom/ns#" term="asynchronous" /><title>SignalR OnDisconnected Task and Dependency Injection with Castle Windsor - Hybrid lifestyle to the rescue</title><content type="html">&lt;p&gt;Today I faced a Castle Windsor dependency injection problem in an ASP.NET MVC app. The problem happened when using &lt;a href="http://www.asp.net/signalr" target="_blank"&gt;SignalR&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;While implementing some UI real time user interaction I needed to have my persistence facility (Database context) injected inside my &lt;a href="https://github.com/SignalR/SignalR/wiki" target="_blank"&gt;SignalR Hub class&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I Googled and found this awesome and to the point post by &lt;a href="http://blog.mirajavora.com/about-me" target="_blank"&gt;Mira Javora&lt;/a&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.mirajavora.com/signalr-dependency-injection" target="_blank"&gt;SignalR-Dependency Injection using Castle.Windsor&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;With the code he provided I was able to configure SignalR’s dependency resolver and everything was working as expected as Mira showed in his post.&lt;/p&gt;  &lt;p&gt;In my case two objects are being injected in the Hub class:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;AssessmentHub &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Hub
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;private &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MyContext _&lt;/span&gt;&lt;span style="background: white; color: black"&gt;database { &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;get&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;set&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;private &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ILogger &lt;/span&gt;&lt;span style="background: white; color: black"&gt;_logger { &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;get&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;set&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: black"&gt;AssessmentHub(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MyContext &lt;/span&gt;&lt;span style="background: white; color: black"&gt;database, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ILogger &lt;/span&gt;&lt;span style="background: white; color: black"&gt;logger)
    {
        _database = database;
        _logger = logger;
    }&lt;br /&gt;    &lt;br /&gt;    ...&lt;br /&gt;}        &lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;They were being properly injected in my own Hub class methods but things started to go wrong when SignalR’s &lt;font face="Courier New"&gt;OnDisconnected&lt;/font&gt; method was being called:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public override &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Task &lt;/span&gt;&lt;span style="background: white; color: black"&gt;OnDisconnected()
{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;try
    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Guid &lt;/span&gt;&lt;span style="background: white; color: black"&gt;connectionId = &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Guid&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Parse(Context.ConnectionId);&lt;br /&gt;
   &lt;span style="background: white; color: green"&gt;     // Database was null here! &lt;img class="wlEmoticon wlEmoticon-sadsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Sad smile" src="http://lh6.ggpht.com/-9LZoveIGwuE/UOS_KOzpAYI/AAAAAAAACh0/wN-Fmyjj12w/wlEmoticon-sadsmile%25255B2%25255D.png?imgmax=800" /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;        var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;client = Database.HubConnections.Single(hc =&amp;gt; hc.ConnectionId == connectionId);

        UnlockChapterAndUpdateClients(client);

        Database.HubConnections.Remove(client);

        Database.SaveChanges();
    }
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;catch &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Exception &lt;/span&gt;&lt;span style="background: white; color: black"&gt;ex)
    {
        Logger.Error(ex.Message, ex);
    }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return base&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.OnDisconnected();
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This was the error I was getting:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;font face="Courier New"&gt;OnDisconnected&lt;/font&gt; method is an asynchronous method and as such it runs under a background thread ( not the one processing the request ). ASP.NET controls the threads and makes sure that only one thread is processing the request. This way &lt;font face="Courier New"&gt;HttpContext&lt;/font&gt; and as a consequence my &lt;font face="Courier New"&gt;_database&lt;/font&gt; injected object is null inside that method.&lt;/p&gt;

&lt;p&gt;After some thinkering I remembered that I had my Persistence Facility registered &lt;font face="Courier New"&gt;PerWebRequest&lt;/font&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;PersistenceFacility &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;AbstractFacility
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;protected override void &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Init()
    {
        Kernel.Register(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Component&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.For&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MyContext&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt;().          
            LifeStyle.PerWebRequest);
    }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Then, I Googled a little bit more and found this StackOverflow question:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://stackoverflow.com/q/11873905/114029" target="_blank"&gt;Castle.Windsor lifestyle depending on context?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;User &lt;a href="http://stackoverflow.com/users/783681/cuong-le" target="_blank"&gt;Cuong Le&lt;/a&gt; shared a great post by &lt;a href="http://stackoverflow.com/users/21239/mauricio-scheffer" target="_blank"&gt;Mauricio Scheffer&lt;/a&gt; called:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://bugsquash.blogspot.com.br/2010/06/hybrid-lifestyles-in-windsor.html" target="_blank"&gt;Hybrid lifestyles in Windsor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;His post describes exactly what I needed (an Hybrid lifestyle ), that is, a way of keeping using the &lt;font face="Courier New"&gt;PerWebRequest&lt;/font&gt; life style when &lt;font face="Courier New"&gt;HttpContext&lt;/font&gt; is available but then resort to &lt;font face="Courier New"&gt;Transient&lt;/font&gt; lifestyle when it’s not.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An hybrid lifestyle is one that actually blends two underlying lifestyles: a main lifestyle and a secondary lifestyle. The hybrid lifestyle first tries to use the main lifestyle; if it's unavailable for some reason, it uses the secondary lifestyle. This is commonly used with PerWebRequest as the main lifestyle: if the HTTP context is available, it's used as the scope for the component instance; otherwise the secondary lifestyle is used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I then grabbed Mauricio’s NuGet package &lt;a href="http://nuget.org/packages/Castle.Windsor.Lifestyles/0.2.0-alpha1" target="_blank"&gt;Castle.Windsor.Lifestyles&lt;/a&gt; and changed my Persistence Facility code to:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;PersistenceFacility &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;AbstractFacility
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;protected override void &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Init()
    {
        Kernel.Register(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Component&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.For&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;MyContext&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt;().          
            LifeStyle.HybridPerWebRequestTransient());
    }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;I’m back in business now. &lt;img class="wlEmoticon wlEmoticon-callme" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Call me" src="http://lh5.ggpht.com/-yusq3p9tKkY/UOS_KtcmZlI/AAAAAAAACh8/PyVwkstVilE/wlEmoticon-callme%25255B2%25255D.png?imgmax=800" /&gt;&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=s-wXC9dHzC8:GFQrEHblBLM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=s-wXC9dHzC8:GFQrEHblBLM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=s-wXC9dHzC8:GFQrEHblBLM:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=s-wXC9dHzC8:GFQrEHblBLM:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=s-wXC9dHzC8:GFQrEHblBLM:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=s-wXC9dHzC8:GFQrEHblBLM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/s-wXC9dHzC8" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7446231153786528716?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7446231153786528716?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/s-wXC9dHzC8/signalr-ondisconnected-task-and-dependency-injection-with-castle.windsor-hybrid-lifestyle.html" title="SignalR OnDisconnected Task and Dependency Injection with Castle Windsor - Hybrid lifestyle to the rescue" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-9LZoveIGwuE/UOS_KOzpAYI/AAAAAAAACh0/wN-Fmyjj12w/s72-c/wlEmoticon-sadsmile%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2013/01/signalr-ondisconnected-task-and-dependency-injection-with-castle.windsor-hybrid-lifestyle.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUMFQXY6fSp7ImA9WhNVGU8.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-8532514689765580521</id><published>2012-12-31T01:02:00.000-02:00</published><updated>2012-12-31T01:10:10.815-02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-31T01:10:10.815-02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="copyright" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="source code" /><category scheme="http://www.blogger.com/atom/ns#" term="PowerShell" /><category scheme="http://www.blogger.com/atom/ns#" term="script" /><title>Updated PowerShell script to insert copyright notice/banner/header in all source code files</title><content type="html">&lt;p&gt;In a recent post I described how one could insert a copyright banner in all source code files located on a given path. You can read all the details about it here: &lt;a href="http://www.leniel.net/2012/08/inserting-copyright-notice-banner-in-all-source-code-files-with-powershell.html" target="_blank"&gt;Inserting copyright notice/banner/header in all source code files with PowerShell script&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;During the past month or so I updated that PowerShell script to give it even more power. In the Notes section of that post I mentioned this point: &lt;/p&gt;  &lt;br /&gt;  &lt;ul&gt;   &lt;li&gt;Beware that if you run the script twice it'll add the copyright notice twice in each file &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;With this updated version (see the highlighted parts) I’ve overcome such limitation.&lt;/p&gt;  &lt;p&gt;I’m now also using the built-in Powershell &lt;em&gt;Filter&lt;/em&gt; keyword to filter the files, that is, processing only .cs files (C# code files) excluding some other files that have the .cs extension but that have a given naming pattern. I exclude those files because it doesn’t make sense to add copyright to them and because in some cases it would break the build.&lt;/p&gt;  &lt;p&gt;Here’s the updated PowerShell script with comments to help you understand what’s going on in each line:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;param&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: purple"&gt;$target &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;C:\MyProject\trunk&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$companyname &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;Leniel’s Software House&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;)

&lt;/span&gt;&lt;span style="color: green"&gt;#[System.Globalization.CultureInfo] $ci = [System.Globalization.CultureInfo]::GetCultureInfo(&amp;quot;pt-BR&amp;quot;)

&lt;/span&gt;&lt;span style="color: black"&gt;[&lt;/span&gt;&lt;span style="color: teal"&gt;System.Globalization.CultureInfo&lt;/span&gt;&lt;span style="color: black"&gt;] &lt;/span&gt;&lt;span style="color: purple"&gt;$ci &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: black"&gt;[&lt;/span&gt;&lt;span style="color: teal"&gt;System.Globalization.CultureInfo&lt;/span&gt;&lt;span style="color: black"&gt;]::GetCurrentCulture

&lt;/span&gt;&lt;span style="color: green"&gt;# Full date pattern with a given CultureInfo
# Look here for available String date patterns: http://www.csharp-examples.net/string-format-datetime/
&lt;/span&gt;&lt;span style="color: purple"&gt;$date &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-Date&lt;/span&gt;&lt;span style="color: black"&gt;).ToString(&lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;F&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$ci&lt;/span&gt;&lt;span style="color: black"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;# Header template
&lt;/span&gt;&lt;span style="color: purple"&gt;$header &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;//-----------------------------------------------------------------------

// &amp;lt;copyright file=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot; company=&amp;quot;&amp;quot;{1}&amp;quot;&amp;quot;&amp;gt;

// Copyright (c) {1}. All rights reserved.

// &amp;lt;author&amp;gt;Leniel Macaferi&amp;lt;/author&amp;gt;

// &amp;lt;date&amp;gt;{2}&amp;lt;/date&amp;gt;

// &amp;lt;/copyright&amp;gt;

//-----------------------------------------------------------------------`r`n&amp;quot;

&lt;/span&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;&lt;span style="color: navy"&gt;Write-Header &lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: purple"&gt;$file&lt;/span&gt;&lt;span style="color: black"&gt;)
{
    &lt;/span&gt;&lt;span style="color: green"&gt;# Get the file content as as Array object that contains the file lines
    &lt;/span&gt;&lt;span style="color: purple"&gt;$content &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file
    
    &lt;/span&gt;&lt;span style="color: green"&gt;# Getting the content as a String
    &lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: purple"&gt;$contentAsString &lt;/span&gt;&lt;span style="color: red"&gt;=  &lt;/span&gt;&lt;span style="color: purple"&gt;$content &lt;/span&gt;&lt;span style="color: black"&gt;| &lt;/span&gt;&lt;/font&gt;&lt;span style="color: #5f9ea0"&gt;&lt;font style="background-color: #ffff00"&gt;Out-String&lt;/font&gt;
    
    &lt;/span&gt;&lt;span style="color: green"&gt;&amp;lt;# If content starts with // then the file has a copyright notice already
       Let's Skip the first 14 lines of the copyright notice template... #&amp;gt;
    &lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: purple"&gt;$contentAsString&lt;/span&gt;&lt;span style="color: black"&gt;.StartsWith(&lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;//&amp;quot;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: black"&gt;&lt;font style="background-color: #ffff00"&gt;))&lt;/font&gt;
    &lt;font style="background-color: #ffff00"&gt;{&lt;/font&gt;
       &lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: purple"&gt;$content &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: purple"&gt;$content &lt;/span&gt;&lt;span style="color: black"&gt;| &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Select-Object -skip &lt;/span&gt;&lt;/font&gt;&lt;span style="color: black"&gt;&lt;font style="background-color: #ffff00"&gt;14&lt;/font&gt;
    &lt;font style="background-color: #ffff00"&gt;}&lt;/font&gt;

    &lt;/span&gt;&lt;span style="color: green"&gt;# Splitting the file path and getting the leaf/last part, that is, the file name
    &lt;/span&gt;&lt;span style="color: purple"&gt;$filename &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Split-Path -Leaf &lt;/span&gt;&lt;span style="color: purple"&gt;$file

    &lt;/span&gt;&lt;span style="color: green"&gt;# $fileheader is assigned the value of $header with dynamic values passed as parameters after -f
    &lt;/span&gt;&lt;span style="color: purple"&gt;$fileheader &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: purple"&gt;$header &lt;/span&gt;&lt;span style="color: red"&gt;-f &lt;/span&gt;&lt;span style="color: purple"&gt;$filename&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$companyname&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$date

    &lt;/span&gt;&lt;span style="color: green"&gt;# Writing the header to the file
    &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Set-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file $fileheader &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;&lt;font style="background-color: #ffff00"&gt;-encoding &lt;/font&gt;&lt;/span&gt;&lt;span style="color: maroon"&gt;&lt;font style="background-color: #ffff00"&gt;UTF8&lt;/font&gt;

    &lt;/span&gt;&lt;span style="color: green"&gt;# Append the content to the file
    &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Add-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file $content
&lt;/span&gt;&lt;span style="color: black"&gt;}

&lt;/span&gt;&lt;span style="color: green"&gt;#Filter files getting only .cs ones and exclude specific file extensions
&lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-ChildItem &lt;/span&gt;&lt;span style="color: purple"&gt;$target &lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: #5f9ea0"&gt;-Filter &lt;/span&gt;&lt;span style="color: black"&gt;*.cs &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;-Exclude &lt;/span&gt;&lt;span style="color: black"&gt;*.Designer.cs,&lt;/span&gt;&lt;span style="color: maroon"&gt;T4MVC.cs&lt;/span&gt;&lt;/font&gt;&lt;span style="color: black"&gt;&lt;font style="background-color: #ffff00"&gt;,*.generated.cs,*.ModelUnbinder.cs&lt;/font&gt; &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;-Recurse &lt;/span&gt;&lt;span style="color: black"&gt;| &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;% `
&lt;/span&gt;&lt;span style="color: black"&gt;{
    &lt;/span&gt;&lt;span style="color: green"&gt;&amp;lt;# For each file on the $target directory that matches the filter,
       let's call the Write-Header function defined above passing the file as parameter #&amp;gt;
    &lt;/span&gt;&lt;span style="color: navy"&gt;Write-Header &lt;/span&gt;&lt;span style="color: purple"&gt;$_&lt;/span&gt;&lt;span style="color: black"&gt;.PSPath.Split(&lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;:&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, 3)[2]
}&lt;/span&gt;&lt;/pre&gt;
Hope you make even better use of such a pearl that comes in handy from time to time. 


  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=H-STIPjSnYc:KGbrvl8QKQE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=H-STIPjSnYc:KGbrvl8QKQE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=H-STIPjSnYc:KGbrvl8QKQE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=H-STIPjSnYc:KGbrvl8QKQE:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=H-STIPjSnYc:KGbrvl8QKQE:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=H-STIPjSnYc:KGbrvl8QKQE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/H-STIPjSnYc" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/8532514689765580521?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/8532514689765580521?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/H-STIPjSnYc/updated-powershell-script-to-insert-copyright-notice-banner-header-in-all-source-code-files.html" title="Updated PowerShell script to insert copyright notice/banner/header in all source code files" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><feedburner:origLink>http://www.leniel.net/2012/12/updated-powershell-script-to-insert-copyright-notice-banner-header-in-all-source-code-files.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEQBSX49cCp7ImA9WhNXGEw.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-250689532633621908</id><published>2012-12-06T11:57:00.000-02:00</published><updated>2012-12-06T14:45:58.068-02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-06T14:45:58.068-02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Mp3tag" /><category scheme="http://www.blogger.com/atom/ns#" term="regular expression" /><category scheme="http://www.blogger.com/atom/ns#" term="MP3" /><category scheme="http://www.blogger.com/atom/ns#" term="metadata" /><category scheme="http://www.blogger.com/atom/ns#" term="replace" /><category scheme="http://www.blogger.com/atom/ns#" term="Regex" /><category scheme="http://www.blogger.com/atom/ns#" term="batch processing" /><category scheme="http://www.blogger.com/atom/ns#" term="guess values" /><title>Mp3tag and its useful actions like Replace with regular expression and Guess values</title><content type="html">&lt;style type="text/css"&gt;



































.container
{
   margin-top: 25px;
}



.usecase
{
 background-color: #ffd904;
    color: black;
    display: inline;
    font-size: 11pt;
    font-style: italic;
    padding: 7px;
}


kbd{
background:none repeat scroll 0 0 #eeeeee;
border-color:#cccccc #aaaaaa #888888 #bbbbbb;
border-style:solid;
border-width:1px 3px 3px 1px;
color:#000000;
padding:2px 4px;
white-space:nowrap;}&lt;/style&gt;  &lt;p&gt;Every once in a while I’m in a situation where I need to refresh my mind about how to work with regular expressions and guess values in Mp3tag to batch process lots of MP3 in a single shot… I’ll try to keep this post as a reference for the artifices I use with Mp3tag so that I can get back here and see &lt;em&gt;what and how&lt;/em&gt; I did to format my MP3 tags the way they should be.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.mp3tag.de/en/index.html" target="_blank"&gt;&lt;img title="Mp3tag - The Universal Tag Editor" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: left; padding-top: 0px; padding-left: 0px; margin: 0px 15px 0px 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="Mp3tag - The Universal Tag Editor" align="left" src="http://lh4.ggpht.com/-4ws4G1BTeSo/UMDBUP53ZqI/AAAAAAAACb4/ENlN1iCRF_8/Mp3tag%25255B4%25255D.gif?imgmax=800" width="252" height="68" /&gt;Mp3tag&lt;/a&gt; is IMHO the best MP3 tag editor out there. I’m so satisfied with it that I stopped trying to find a better tool. Nonetheless, I’m open to recommendations…&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Mp3tag is a powerful and yet easy-to-use tool to edit metadata of common audio formats where it supports ID3v1, ID3v2.3, ID3v2.4, iTunes MP4, WMA, Vorbis Comments and APE Tags.&lt;/p&gt;    &lt;p&gt;It can rename files based on the tag information, replace characters or words in tags and filenames, import/export tag information, create playlists and &lt;a href="http://www.mp3tag.de/en/index.html#features" target="_blank"&gt;more&lt;/a&gt;.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I’ve been writing about &lt;em&gt;MP3&lt;/em&gt; in this blog. If you’re interested, you can check past posts &lt;a href="http://www.leniel.net/search/label/MP3" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As I commented above, to refresh my mind I tend to look for this post I wrote some time ago: &lt;a href="http://www.leniel.net/2011/05/more-mp3-guessing-pattern-mp3tag-mac-os.html" target="_blank"&gt;More MP3 guessing pattern with Mp3tag in Mac OS&lt;/a&gt; but I decided to compile future endeavors in this area in a single post. I hope you enjoy.&lt;/p&gt;  &lt;p&gt;This time I’m using Mp3tag in the Windows 8 side inside a &lt;a href="http://www.leniel.net/2010/09/developing-for-iphone-with-mac-mini.html" target="_blank"&gt;Parallels Desktop&lt;/a&gt; virtual machine.&lt;/p&gt;  &lt;p&gt;OK. After some formalities, let’s complete these five basic steps needed in every use case I’ll present in this post:&lt;/p&gt;  &lt;p&gt;1 - Click the change directory button and navigate to the folder where you store the MP3s you want to edit.&lt;/p&gt;  &lt;p&gt;&lt;img title="Mp3tag Change Directory button" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Change Directory button" src="http://lh5.ggpht.com/-toLWNnp9HC8/UMCmoriu1zI/AAAAAAAACa8/nY1k4RXk09U/Mp3tagChangeDirectory_thumb%25255B2%25255D%25255B5%25255D.png?imgmax=800" width="244" height="115" /&gt; &lt;/p&gt;  &lt;p&gt;2 - Select the files you want to edit. I just press &lt;kbd&gt;Ctrl&lt;/kbd&gt; + &lt;kbd&gt;A&lt;/kbd&gt; to select all the files. You can also hold &lt;kbd&gt;Ctrl&lt;/kbd&gt; to select file by file.&lt;/p&gt;  &lt;p&gt;3 - Click the Actions (Quick) button.&lt;/p&gt;  &lt;p&gt;&lt;img title="Mp3tag Actions (Quick) button" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Actions (Quick) button" src="http://lh5.ggpht.com/-Re3CLIcQsiM/UMC4KUB_SbI/AAAAAAAACbU/lnsTVZZX-Dg/Mp3tagActionsQuick%25255B14%25255D.png?imgmax=800" width="244" height="115" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;4 - Follow the use cases…&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;a href="http://www.leniel.net/2012/12/mp3tag-and-its-useful-actions-like-replace-with-regular-expression-and-guess-values.html#usecase1"&gt;Removing year between parenthesis from file name&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;a href="http://www.leniel.net/2012/12/mp3tag-and-its-useful-actions-like-replace-with-regular-expression-and-guess-values.html#usecase2"&gt;Guessing values for Artist and Title from file name&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;5 - &lt;font color="#ff0000"&gt;&lt;em&gt;DO NOT FORGET&lt;/em&gt;&lt;/font&gt; to click the Save button after executing the actions of each use case so that the changes get applied to the files; otherwise you’ll lose the edits. &lt;img class="wlEmoticon wlEmoticon-confusedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Confused smile" src="http://lh5.ggpht.com/-2oIaDj6CUl0/UMDJCndK1GI/AAAAAAAACcM/qvfKuH_6jA8/wlEmoticon-confusedsmile%25255B2%25255D.png?imgmax=800" /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="Mp3tag Save button" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Save button" src="http://lh6.ggpht.com/-yafIdpG2ZeM/UMDJEQMYnxI/AAAAAAAACck/w1plLt5oyqU/Mp3tagSave%25255B5%25255D.png?imgmax=800" width="244" height="115" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Use cases&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;a name="usecase1"&gt;   &lt;div class="container"&gt;     &lt;div class="usecase"&gt;Removing year between parenthesis from file name&lt;/div&gt;   &lt;/div&gt; &lt;/a&gt;  &lt;p&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;img title="Mp3tag Filename with Year between parenthesis" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Filename with Year between parenthesis" src="http://lh6.ggpht.com/-PidGgpGeXMg/UMCmrkB75cI/AAAAAAAACbA/Qef_n_QN16E/Mp3tagFilenameWithYearBetweenParenthesis_thumb%25255B5%25255D%25255B5%25255D.png?imgmax=800" width="600" height="400" /&gt;&lt;/p&gt;  &lt;p&gt;After clicking the Actions button, select Replace with regular expression, select the &lt;font size="2" face="Courier New"&gt;_FILENAME&lt;/font&gt; field and enter the regular expression &lt;font size="2" face="Courier New"&gt;\( 2o12 \)&lt;/font&gt;. Press OK and you’re done:&lt;/p&gt;  &lt;p&gt;&lt;img title="Mp3tag File name with Year between parenthesis regex" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag File name with Year between parenthesis regex" src="http://lh4.ggpht.com/-2GBhDkZszxk/UMCmt30Mo-I/AAAAAAAACbE/7w1ivZwioCk/Mp3tagFilenameWithYearBetweenParenthesisRegex_thumb%25255B2%25255D%25255B5%25255D.png?imgmax=800" width="342" height="257" /&gt;&lt;/p&gt;  &lt;a name="usecase2"&gt; &lt;div class="container"&gt;   &lt;div class="usecase"&gt;Guessing values for Artist and Title from File name&lt;/div&gt; &lt;/div&gt;&lt;/a&gt;  &lt;p&gt;&lt;img title="Mp3tag Guess Values for Artist and Title from File name" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Guess Values for Artist and Title from File name" src="http://lh4.ggpht.com/-lbnqEiQv8vI/UMC4Lo8dhuI/AAAAAAAACbc/UJdOYx1KpqE/Mp3tagGuessValuesForArtistAndTitleFromFileName%25255B5%25255D.png?imgmax=800" width="618" height="400" /&gt;&lt;/p&gt;  &lt;p&gt;As you see the MP3 files don’t have Artist and Title information. This is bad. If you use services like &lt;a href="http://www.last.fm/user/leniel" target="_blank"&gt;Last.fm&lt;/a&gt; to keep track of the music you’ve been listening to, you won’t be able to scrobble given the missing metadata. Of course you can fill the info by hand (Oh Lord! How boring and time consuming this task is). There’s Mp3tag to the rescue.&lt;/p&gt;  &lt;p&gt;Taking the previous use case as a necessary step to format the file name accordingly…&lt;/p&gt;  &lt;p&gt;After clicking the Actions button, select Guess values, in Source format enter &lt;font size="2" face="Courier New"&gt;%_filename%&lt;/font&gt;, in Guessing pattern enter &lt;font size="2" face="Courier New"&gt;\%artist% - %title%&lt;/font&gt;. Press OK and you’re done:&lt;/p&gt;  &lt;p&gt;&lt;img title="Mp3tag Guess Values for Artist and Title from File name regex" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Mp3tag Guess Values for Artist and Title from File name regex" src="http://lh5.ggpht.com/-vc8seNmSq9s/UMC4MjveyaI/AAAAAAAACbk/HRrMHaMDmhA/Mp3tagGuessValuesForArtistAndTitleFromFileNameRegex%25255B12%25255D.png?imgmax=800" width="339" height="192" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://help.mp3tag.de/" target="_blank"&gt;MP3 Tag - The Universal Tag Editor Help pages&lt;/a&gt;&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=b9yOBTgpEPg:celRi_OXKNo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=b9yOBTgpEPg:celRi_OXKNo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=b9yOBTgpEPg:celRi_OXKNo:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=b9yOBTgpEPg:celRi_OXKNo:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=b9yOBTgpEPg:celRi_OXKNo:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=b9yOBTgpEPg:celRi_OXKNo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/b9yOBTgpEPg" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/250689532633621908?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/250689532633621908?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/b9yOBTgpEPg/mp3tag-and-its-useful-actions-like-replace-with-regular-expression-and-guess-values.html" title="Mp3tag and its useful actions like Replace with regular expression and Guess values" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-4ws4G1BTeSo/UMDBUP53ZqI/AAAAAAAACb4/ENlN1iCRF_8/s72-c/Mp3tag%25255B4%25255D.gif?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/12/mp3tag-and-its-useful-actions-like-replace-with-regular-expression-and-guess-values.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YBQ3g7fip7ImA9WhNTEEg.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-6690184531261478933</id><published>2012-10-12T12:03:00.000-03:00</published><updated>2012-10-12T13:25:52.606-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-12T13:25:52.606-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="alphabet" /><category scheme="http://www.blogger.com/atom/ns#" term="digits" /><category scheme="http://www.blogger.com/atom/ns#" term="input" /><category scheme="http://www.blogger.com/atom/ns#" term="textbox" /><category scheme="http://www.blogger.com/atom/ns#" term="Regex" /><category scheme="http://www.blogger.com/atom/ns#" term="numbers" /><category scheme="http://www.blogger.com/atom/ns#" term="character set" /><category scheme="http://www.blogger.com/atom/ns#" term="letters" /><category scheme="http://www.blogger.com/atom/ns#" term="jQuery" /><category scheme="http://www.blogger.com/atom/ns#" term="English" /><title>JavaScript regex + jQuery to allow only English chars/letters in input textbox</title><content type="html">&lt;p&gt;Yesterday I &lt;a href="http://stackoverflow.com/a/12844565/114029" target="_blank"&gt;answered&lt;/a&gt; this &lt;a href="http://stackoverflow.com/q/12844477/114029" target="_blank"&gt;question&lt;/a&gt; at StackOverflow. The questioner wanted this:&lt;/p&gt;  &lt;p align="center"&gt;&lt;em&gt;How to allow only English, numeric and special chars in textbox using jQuery or JavaScript?&lt;/em&gt;&lt;/p&gt;  &lt;p align="left"&gt;My answer was this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#mytextbox'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).bind(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'keyup blur'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;() {
    $(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;this&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).val($(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;this&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).val().replace(&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;/[^A-Za-z]/g&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;''&lt;/span&gt;&lt;span style="background: white; color: black"&gt;))
});&lt;/span&gt;&lt;/pre&gt;


&lt;p align="left"&gt;It has a flaw because if you test it, you’ll see that every time a disallowed character is typed, the keyboard &lt;a href="http://en.wikipedia.org/wiki/Caret_navigation" target="_blank"&gt;caret&lt;/a&gt;/&lt;a href="http://en.wikipedia.org/wiki/Cursor_%28computers%29" target="_blank"&gt;text cursor&lt;/a&gt; goes to the end of the textbox. This is something that shouldn’t happen. &lt;a href="http://stackoverflow.com/users/159270/aristos" target="_blank"&gt;Aristos&lt;/a&gt; mentioned something that I hadn’t tested: “the problem with your answer is that if you try to type something in the middle of the text, the cursor moves to the end.” He is absolutely right. You can test it below. Click the Result tab and type a digit like 7 or some other disallowed char in the middle of the textbox text to spot the problem:&lt;/p&gt;

&lt;p&gt;&lt;iframe style="height: 100px; width: 100%" src="http://jsfiddle.net/leniel/rtE54/3/embedded/js,html,result" frameborder="0" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;p align="left"&gt;OK – of course there must be a solution to this annoying problem and I couldn't miss the opportunity to play with it to find something that just works as expected. This &lt;a href="http://stackoverflow.com/a/2564127/114029" target="_blank"&gt;answer&lt;/a&gt; by &lt;a href="http://stackoverflow.com/users/107820/ender" target="_blank"&gt;Ender&lt;/a&gt; helped me get there. The following code is what I came up with. I commented the code so it should be easy to understand what's going on:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#mytextbox&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).on(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;keypress&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event) {

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;englishAlphabetAndWhiteSpace = &lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;/[A-Za-z ]/g&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Retrieving the key from the char code passed in event.which
    // For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;key = String.fromCharCode(event.which);

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;//alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
    // keyCode == 8  is backspace
    // keyCode == 37 is left arrow
    // keyCode == 39 is right arrow
    // englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return true&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
    }

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// If we got this far, just return false because a disallowed key was typed.
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
});

$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#mytextbox'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).on(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;paste&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(e) {
    e.preventDefault();
});&lt;/span&gt;&lt;/pre&gt;


&lt;p align="left"&gt;Try it below:&lt;/p&gt;

&lt;p align="left"&gt;&lt;iframe style="height: 575px; width: 100%" src="http://jsfiddle.net/leniel/Fn9cy/7/embedded/js,html,result/" frameborder="0" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;p&gt;Now you can type in the middle of the text and the text cursor should maintain its position.&lt;/p&gt;

&lt;p&gt;The above version allows accented chars like á, é, â, ê, etc…&lt;/p&gt;

&lt;p&gt;Note also that there’s a new event handler function (paste) attached to the input textbox. This removes the possibility of user pasting text in the textbox.&lt;/p&gt;

&lt;p&gt;If you want to allow numbers/digits or any other special characters, it’s just a matter of updating the regular expression pattern. For example: if you want to allow digits from 0 to 9, just use this regex pattern:&lt;/p&gt;

&lt;p&gt;&lt;font size="2" face="Courier New"&gt;var englishAlphabetAndDigits = /[A-Za-z0-9 ]/g;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Full code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#mytextbox&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).on(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;keypress&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event) {

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase, digits 0 to 9 and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;englishAlphabetDigitsAndWhiteSpace = &lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;/[A-Za-z0-9 ]/g&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Retrieving the key from the char code passed in event.which
    // For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;key = String.fromCharCode(event.which);

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;//alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
    // keyCode == 8  is backspace
    // keyCode == 37 is left arrow
    // keyCode == 39 is right arrow
    // englishAlphabetDigitsAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetDigitsAndWhiteSpace.test(key)) {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return true&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
    }

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// If we got this far, just return false because a disallowed key was typed.
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
});

$(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'#mytextbox'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).on(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;paste&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(e) {
    e.preventDefault();
});&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Here’s an updated jsFiddle that allows digits:&lt;/p&gt;

&lt;p&gt;&lt;iframe style="height: 575px; width: 100%" src="http://jsfiddle.net/leniel/f279M/4/embedded/js,html,result" frameborder="0" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;p&gt;It’s really impressive what some small amount of JavaScript and jQuery code allows you to do on the client side. Actually if you care there are more comments than codez… &lt;img class="wlEmoticon wlEmoticon-surprisedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Surprised smile" src="http://lh4.ggpht.com/-qvLRsHc1uLw/UHhCeUD--RI/AAAAAAAACZQ/gHKCRygOd7c/wlEmoticon-surprisedsmile%25255B2%25255D.png?imgmax=800" /&gt;&lt;/p&gt;

&lt;p&gt;There’s no doubt JavaScript and JQuery will reign in the years to come.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the code 
    &lt;br /&gt;&lt;/strong&gt;As everything in software the above piece of code needs more testing. If you find a bug or some functionality that is missing, just leave a comment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References 
    &lt;br /&gt;&lt;/strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions" target="_blank"&gt;JavaScript Regular Expressions Guide&lt;/a&gt; by Mozzila.org&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Vk5pmsJSTSk:6jE4kYSt3C4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Vk5pmsJSTSk:6jE4kYSt3C4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Vk5pmsJSTSk:6jE4kYSt3C4:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Vk5pmsJSTSk:6jE4kYSt3C4:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=Vk5pmsJSTSk:6jE4kYSt3C4:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=Vk5pmsJSTSk:6jE4kYSt3C4:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/Vk5pmsJSTSk" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/6690184531261478933?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/6690184531261478933?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/Vk5pmsJSTSk/javascript-regex-jquery-to-match-only-english-characters-in-input-textbox.html" title="JavaScript regex + jQuery to allow only English chars/letters in input textbox" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-qvLRsHc1uLw/UHhCeUD--RI/AAAAAAAACZQ/gHKCRygOd7c/s72-c/wlEmoticon-surprisedsmile%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/10/javascript-regex-jquery-to-match-only-english-characters-in-input-textbox.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUcFSXozfyp7ImA9WhJaGUs.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-7381129404413616280</id><published>2012-10-10T18:27:00.001-03:00</published><updated>2012-10-11T11:50:18.487-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-11T11:50:18.487-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Blogger" /><category scheme="http://www.blogger.com/atom/ns#" term="delete" /><category scheme="http://www.blogger.com/atom/ns#" term="template" /><category scheme="http://www.blogger.com/atom/ns#" term="widget" /><category scheme="http://www.blogger.com/atom/ns#" term="navbar" /><category scheme="http://www.blogger.com/atom/ns#" term="HTML" /><title>Delete Blogger Navbar section/widget from your blog template code</title><content type="html">&lt;p&gt;In the past I used to just hide &lt;a href="http://support.google.com/blogger/bin/answer.py?hl=en&amp;amp;answer=42269" target="_blank"&gt;Blogger’s navbar widget&lt;/a&gt; with some &lt;a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets" target="_blank"&gt;CSS&lt;/a&gt; code but it was still there being loaded in the HTML code. This approach is inefficient because I really don’t want the navbar. So why should it still be there in the code taking some precious time to load and impacting the user experience?&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Blogger navbar" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Blogger navbar" src="http://lh6.ggpht.com/-llTwRcv1mks/UHXvnpqW_bI/AAAAAAAACVw/3lqZ_xSXMic/BloggerNavbar%25255B5%25255D.png?imgmax=800" width="531" height="165" /&gt;Figure 1 - Blogger navbar&lt;/p&gt;  &lt;p&gt;In my case when I looked at the Layout section of my blog in &lt;a href="http://draft.blogger.com/" target="_blank"&gt;Blogger dashboard&lt;/a&gt; I saw that the navbar section was locked (see the &lt;font size="2" face="Courier New"&gt;locked=’true’&lt;/font&gt; in &lt;a href="http://www.leniel.net/2012/10/delete-remove-blogger-navbar-section-widget-from-your-blog-template-html-markup-code.html#Figure2"&gt;Figure 2&lt;/a&gt;) which means I could not delete it in the Layout screen. So there’s only one option to really get rid of it and it is through the widget code.&lt;/p&gt;  &lt;p&gt;If you look carefully you’ll note that the navbar’s code loads some JavaScript files as the &lt;a href="https://developers.google.com/+/plugins/+1button" target="_blank"&gt;Google Plus One&lt;/a&gt; &lt;font size="2" face="Courier New"&gt;plusone.js&lt;/font&gt; file. This is impacting my page load speed because I’m already loading the plusone script somewhere else since I have a customized template. This leads to duplicate requests for the same JavaScript file. This is really really bad. There’s no need for it.&lt;/p&gt;  &lt;p&gt;So I was after a way of removing the navbar forever; not just hiding it with CSS. I then found &lt;a href="http://www.techtutshub.com/2012/02/how-to-remove-navbar-completely-in.html" target="_blank"&gt;a way&lt;/a&gt; of doing it but it didn’t work for me. Blogger redesigned its interface and maybe this is the problem.&lt;/p&gt;  &lt;p&gt;Then I just thought: what if I try to select the navbar section code and delete it and hit save on the HTML template – who knows… this can work out. What’s the result: it does work out.&lt;/p&gt;  &lt;p&gt;How to do it? Follow these 10 simple steps…&lt;/p&gt;  &lt;p&gt;1 - First save a backup copy of your blog template for the case where something goes wrong;&lt;/p&gt;  &lt;p&gt;2 - Go to &lt;a href="http://draft.blogger.com/" target="_blank"&gt;Blogger dashboard&lt;/a&gt; and select Template in the menu on the left;&lt;/p&gt;  &lt;p&gt;3 - Hit the Backup / Restore button at the screen top right and save a backup copy of your layout;&lt;/p&gt;  &lt;p&gt;4 - In the Template screen, click Edit HTML button;&lt;/p&gt;  &lt;p&gt;5 - Click the checkbox Expand Widget Templates;&lt;/p&gt;  &lt;p&gt;6 - Hit Ctrl + F and find the string ‘navbar’;&lt;/p&gt;  &lt;p&gt;7 - Select the section code like the one shown in the following screenshot:&lt;/p&gt;  &lt;p align="center"&gt;&lt;a name="Figure2"&gt;&lt;img title="Selecting the navbar section/widget code while editing the blog’s template HTML code" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Selecting the navbar section/widget code while editing the blog’s template HTML code" src="http://lh4.ggpht.com/-cPKC4kwlbaQ/UHXsJHVaHTI/AAAAAAAACVc/_W6OCMP1dGw/BloggerTemplateEditHtmlNavbarSection%25255B2%25255D%25255B6%25255D.png?imgmax=800" width="625" height="459" /&gt;&lt;/a&gt;Figure 2 - Selecting the navbar section/widget code while editing the blog’s template HTML code&lt;/p&gt;  &lt;p&gt;8 - Hit delete on the keyboard;&lt;/p&gt;  &lt;p&gt;9 - Click the Save template button in orange. You should get this message:&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Warning message informing that deleting the navbar widget cannot be undone" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Warning message informing that deleting the navbar widget cannot be undone" src="http://lh4.ggpht.com/-iiyrejexSUs/UHXsLj2svnI/AAAAAAAACVg/6lNIFNeZ75M/DeleteNavbarWidgetWarningMessage_thumb%25255B4%25255D%25255B5%25255D.png?imgmax=800" width="625" height="115" /&gt;Figure 3 - Warning message informing that deleting the navbar widget cannot be undone&lt;/p&gt;  &lt;p&gt;10 - Click Delete widgets button and you’re done. &lt;img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Smile" src="http://lh4.ggpht.com/-5QxShF5qAek/UHXsMdv9p2I/AAAAAAAACVQ/K67W0Cam91g/wlEmoticon-smile%25255B3%25255D.png?imgmax=800" /&gt;&lt;/p&gt;  &lt;p&gt;Now your page will load a bit faster and this is pretty nice for the user visiting your blog.&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=SnqKah0ZDyM:3L38W720KpY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=SnqKah0ZDyM:3L38W720KpY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=SnqKah0ZDyM:3L38W720KpY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=SnqKah0ZDyM:3L38W720KpY:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=SnqKah0ZDyM:3L38W720KpY:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=SnqKah0ZDyM:3L38W720KpY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/SnqKah0ZDyM" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7381129404413616280?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/7381129404413616280?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/SnqKah0ZDyM/delete-remove-blogger-navbar-section-widget-from-your-blog-template-html-markup-code.html" title="Delete Blogger Navbar section/widget from your blog template code" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-llTwRcv1mks/UHXvnpqW_bI/AAAAAAAACVw/3lqZ_xSXMic/s72-c/BloggerNavbar%25255B5%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/10/delete-remove-blogger-navbar-section-widget-from-your-blog-template-html-markup-code.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4MQHc8fCp7ImA9WhNVGU8.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-213880167181599914</id><published>2012-08-30T14:17:00.001-03:00</published><updated>2012-12-31T01:19:41.974-02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-31T01:19:41.974-02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="copyright" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="source code" /><category scheme="http://www.blogger.com/atom/ns#" term="PowerShell" /><category scheme="http://www.blogger.com/atom/ns#" term="script" /><title>Inserting copyright notice/banner/header in all source code files with PowerShell script</title><content type="html">&lt;p align="center"&gt;&lt;em&gt;I have a better version of this PowerShell script if you’re interested.     &lt;br /&gt;I suggest you read this post first (there’s a bonus point) then go take a look at the new one here:      &lt;br /&gt;&lt;/em&gt;&lt;a href="http://www.leniel.net/2012/12/updated-powershell-script-to-insert-copyright-notice-banner-header-in-all-source-code-files.html" target="_blank"&gt;Updated PowerShell script to insert copyright notice/banner/header in all source code files&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Today my client asked for the source code of a project we worked together. He wants to submit it to patent review and he wanted to include a copyright notice in each C# code file (.cs).&lt;/p&gt;  &lt;p&gt;I googled about a way of accomplishing this and found this post:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.andyparkhill.co.uk/2010/07/use-visual-studio-macro-to-insert.html" target="_blank"&gt;Use a Visual Studio Macro to Insert Copyright Headers into Source Files&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It looked promising but as I’m working with Visual Studio 2012 I just hit a road block because it dropped the support for Macros. So I had to find some other way… in the meantime I asked a &lt;a href="http://stackoverflow.com/q/12199409/114029" target="_blank"&gt;question&lt;/a&gt; at StackOverflow to see if someone had any good advice regarding this. Right now there’s one nice &lt;a href="http://stackoverflow.com/a/12200296/114029" target="_blank"&gt;answer&lt;/a&gt; but it requires manual work. I’m really interested in an automated process (do the work in a batch fashion) because there are lots of .cs files in the Visual Studio solution comprised of 7 projects.&lt;/p&gt;  &lt;p&gt;Some more Googling with the right words (read &lt;a href="http://en.wikipedia.org/wiki/Windows_PowerShell" target="_blank"&gt;PowerShell&lt;/a&gt;) and I was able to find something that could do the work. Kishor Aher wrote about it &lt;a href="http://kishordaher.wordpress.com/2010/03/11/powershell-copyright-header-generator-script/" target="_blank"&gt;Powershell – Copyright header generator script&lt;/a&gt;. Oh God, the internet is really amazing thingy! Remember to just search for the right words…&lt;/p&gt;  &lt;p&gt;Now it was just a matter of adapting the PowerShell script to my needs. Here is it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;param&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: purple"&gt;$target &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&lt;font style="background-color: #ffff00"&gt;&amp;quot;C:\MyProject&amp;quot;&lt;/font&gt;&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$companyname &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;My Company&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, &lt;/span&gt;&lt;span style="color: purple"&gt;$date &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-Date&lt;/span&gt;&lt;span style="color: black"&gt;))

&lt;/span&gt;&lt;span style="color: purple"&gt;$header &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;//-----------------------------------------------------------------------

// &amp;lt;copyright file=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot; company=&amp;quot;&amp;quot;{1}&amp;quot;&amp;quot;&amp;gt;

// Copyright (c) {1}. All rights reserved.

// &amp;lt;author&amp;gt;Leniel Macaferi&amp;lt;/author&amp;gt;

// &amp;lt;date&amp;gt;{2}&amp;lt;/date&amp;gt;

// &amp;lt;/copyright&amp;gt;

//-----------------------------------------------------------------------`r`n&amp;quot;

&lt;/span&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;&lt;span style="color: navy"&gt;Write-Header &lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: purple"&gt;$file&lt;/span&gt;&lt;span style="color: black"&gt;)
{
    &lt;/span&gt;&lt;span style="color: purple"&gt;$content &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file

    $filename &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Split-Path -Leaf &lt;/span&gt;&lt;span style="color: purple"&gt;$file

    $fileheader &lt;/span&gt;&lt;span style="color: red"&gt;= &lt;/span&gt;&lt;span style="color: purple"&gt;$header &lt;/span&gt;&lt;span style="color: red"&gt;-f &lt;/span&gt;&lt;span style="color: purple"&gt;$filename&lt;/span&gt;&lt;span style="color: black"&gt;,&lt;/span&gt;&lt;span style="color: purple"&gt;$companyname&lt;/span&gt;&lt;span style="color: black"&gt;,&lt;/span&gt;&lt;span style="color: purple"&gt;$date

    &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Set-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file $fileheader

    &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Add-Content &lt;/span&gt;&lt;span style="color: purple"&gt;$file $content
&lt;/span&gt;&lt;span style="color: black"&gt;}

&lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;Get-ChildItem &lt;/span&gt;&lt;span style="color: purple"&gt;$target &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;-Recurse &lt;/span&gt;&lt;span style="color: black"&gt;| ? { &lt;/span&gt;&lt;span style="color: purple"&gt;$_&lt;/span&gt;&lt;span style="color: black"&gt;.Extension &lt;/span&gt;&lt;span style="color: red"&gt;-like &lt;/span&gt;&lt;span style="color: maroon"&gt;&lt;font style="background-color: #ffff00"&gt;&amp;quot;.cs&amp;quot;&lt;/font&gt; &lt;/span&gt;&lt;span style="color: black"&gt;} | &lt;/span&gt;&lt;span style="color: #5f9ea0"&gt;% `
&lt;/span&gt;&lt;span style="color: black"&gt;{
    &lt;/span&gt;&lt;span style="color: navy"&gt;Write-Header &lt;/span&gt;&lt;span style="color: purple"&gt;$_&lt;/span&gt;&lt;span style="color: black"&gt;.PSPath.Split(&lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;:&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, 3)[2]
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As you see above, the script will place the copyright notice in every &lt;font size="2" face="Courier New"&gt;.cs&lt;/font&gt; file that lies within the folder &lt;font size="2" face="Courier New"&gt;C:\MyProject&lt;/font&gt;. So let’s say you have a Visual Studio solution with various projects – the script will traverse all files placing the notice in every .cs file it finds along the way…&lt;/p&gt;

&lt;p&gt;I named the script &lt;font size="2" face="Courier New"&gt;Copyright.ps1&lt;/font&gt;.&lt;/p&gt;

&lt;p&gt;To make this work I just opened PowerShell command line, navigated to the folder where I placed the script file and typed &lt;font size="2" face="Courier New"&gt;.\Copyright.ps1&lt;/font&gt; to run the script:&lt;/p&gt;

&lt;p align="center"&gt;&lt;img title="Executing Copyright PowerShell script using PowerShell command line tool" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Executing Copyright PowerShell script using PowerShell command line tool" src="http://lh6.ggpht.com/-u6aOzSr0NYk/UD-gnixAIBI/AAAAAAAACQE/keNJwG_i50o/ExecutingCopyrightPowerShellScript_thumb%25255B2%25255D%25255B5%25255D.png?imgmax=800" width="569" height="384" /&gt;Figure 1 - Executing Copyright PowerShell script using PowerShell command line tool&lt;/p&gt;

&lt;p&gt;You’re done!&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Close Visual Studio solution before running the PowerShell script. Otherwise you may get some error message telling you that some file is in use. &lt;/li&gt;

  &lt;li&gt;Be sure to have your source code files commited in a &lt;a href="http://www.leniel.net/2011/05/svn-hudson-msbuild-local-rev-control.html" target="_blank"&gt;source control repository&lt;/a&gt; before running the scripts so that if anything goes wrong you can just revert to the previous state. &lt;/li&gt;

  &lt;li&gt;Beware that if you run the script twice it'll add the copyright notice twice in each file. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A big thank you to Kishor for providing us the PowerShell script. I’m not that experienced with PowerShell but adapting existing scripts is something easy to do IMHO.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus 
    &lt;br /&gt;&lt;/strong&gt;To get support for PowerShell scripting in Visual Studio with syntax highlighting and IntelliSense, there’s an awesome and free product out there. It’s called &lt;a href="http://powergui.org/index.jspa" target="_blank"&gt;PowerGUI&lt;/a&gt;. In order to use it, download PowerGUI &lt;a href="http://powergui.org/downloads.jspa" target="_blank"&gt;here&lt;/a&gt; and then install its accompanying Visual Studio extension available &lt;a href="http://powerguivsx.codeplex.com/releases/view/87207" target="_blank"&gt;here&lt;/a&gt;. As I’m using Visual Studio 2012 I had to install the alpha release available &lt;a href="http://powerguivsx.codeplex.com/releases/view/86007" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See for yourself the awesomeness:&lt;/p&gt;

&lt;p align="center"&gt;&lt;a href="http://lh3.ggpht.com/-esNdhrmOuzg/UD-l6SkGtII/AAAAAAAACP0/N9140co0jI8/s1600-h/PowerGUIPowerShellScriptIntelliSenseCodeHighlighting%25255B5%25255D.png"&gt;&lt;img title="PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting" src="http://lh5.ggpht.com/-SH4JqOPhyTY/UD-l8NExwtI/AAAAAAAACP8/XPEfUOmIrsM/PowerGUIPowerShellScriptIntelliSenseCodeHighlighting_thumb%25255B3%25255D.png?imgmax=800" width="625" height="542" /&gt;&lt;/a&gt; 

  &lt;br /&gt;Figure 2 - PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting&lt;/p&gt;&lt;/strong&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=gb1xyRZpQ4E:MeWKmwtpId0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=gb1xyRZpQ4E:MeWKmwtpId0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=gb1xyRZpQ4E:MeWKmwtpId0:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=gb1xyRZpQ4E:MeWKmwtpId0:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=gb1xyRZpQ4E:MeWKmwtpId0:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=gb1xyRZpQ4E:MeWKmwtpId0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/gb1xyRZpQ4E" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/213880167181599914?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/213880167181599914?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/gb1xyRZpQ4E/inserting-copyright-notice-banner-in-all-source-code-files-with-powershell.html" title="Inserting copyright notice/banner/header in all source code files with PowerShell script" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-u6aOzSr0NYk/UD-gnixAIBI/AAAAAAAACQE/keNJwG_i50o/s72-c/ExecutingCopyrightPowerShellScript_thumb%25255B2%25255D%25255B5%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/08/inserting-copyright-notice-banner-in-all-source-code-files-with-powershell.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYNQXs6fip7ImA9WhJWEkk.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-4986841472940833012</id><published>2012-08-17T20:13:00.000-03:00</published><updated>2012-08-17T21:03:10.516-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-08-17T21:03:10.516-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dynamic" /><category scheme="http://www.blogger.com/atom/ns#" term="format" /><category scheme="http://www.blogger.com/atom/ns#" term="text" /><category scheme="http://www.blogger.com/atom/ns#" term="monospaced" /><category scheme="http://www.blogger.com/atom/ns#" term="alignment" /><category scheme="http://www.blogger.com/atom/ns#" term="font" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="CSS" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="string" /><title>string.Format with dynamic length left aligned text in C#</title><content type="html">&lt;p&gt;This post is about dynamic text alignment with C#.&lt;/p&gt;  &lt;p&gt;Scenario: I wanted to implement a dropdownlist with list items left aligned based on a string format. This is the initial appearance of the dropdown with no text alignment applied:&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="DropDownList with Text not aligned" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="DropDownList with Text not aligned" src="http://lh4.ggpht.com/-uNeSSqwI_zc/UC7PtkGTarI/AAAAAAAACO8/hZeii_iJBco/DropdownlistTextRightAlignedBefore%25255B5%25255D.png?imgmax=800" width="437" height="115" /&gt;Figure 1 - DropDownList with Text not aligned&lt;/p&gt;  &lt;p align="left"&gt;See that because Hospital names that come before &lt;font size="2" face="Courier New"&gt;=&amp;gt;&lt;/font&gt; have different lengths, it doesn’t look good. Start and End dates are not aligned due to this.&lt;/p&gt;  &lt;p&gt;So I thought about adding some caring to this and remembered that C# supports what I want to do. It’s called &lt;a href="http://msdn.microsoft.com/en-us/library/txafckwd.aspx" target="_blank"&gt;Composite Formatting&lt;/a&gt;. This when used with the Alignment Component allows some nice implementations.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The optional &lt;em&gt;alignment&lt;/em&gt; component is a signed integer indicating the preferred formatted field width. If the value of &lt;em&gt;alignment&lt;/em&gt; is less than the length of the formatted string, &lt;em&gt;alignment&lt;/em&gt; is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if &lt;em&gt;alignment&lt;/em&gt; is positive and left-aligned if &lt;em&gt;alignment&lt;/em&gt; is negative. If padding is necessary, white space is used. The comma is required if &lt;em&gt;alignment &lt;/em&gt;is specified.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Look at the following action method that lies within an &lt;a href="http://www.asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; app. It’s responsible for adding list items to the dropdown show above…&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;GET&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Reports&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)]
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ActionResult &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Index()
{
    &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ReportViewModel &lt;/span&gt;&lt;span style="background: white; color: black"&gt;model = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ReportViewModel&lt;/span&gt;&lt;span style="background: white; color: black"&gt;();

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Getting the length of the lengthiest FictitiousName using Max query operator
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;maxLength = Database.Assessments.Max(a =&amp;gt; a.Hospital.FictitiousName.Length);

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Here’s the dynamic part: maxLength is used inside a format string to right align all strings, hence the minus sign. Doing this all strings will be equally left aligned (by maxLength) no matter their size.
    // {0}, {1} and {2} are positional parameters.
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string &lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;font style="background-color: #ffff00"&gt;format&lt;/font&gt; = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;{0, -&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;+ maxLength + &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;} =&amp;gt; {1} - {2}&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

    &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;SelectListItem&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt; items = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;SelectListItem&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt;();

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;foreach&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Assessment &lt;/span&gt;&lt;span style="background: white; color: black"&gt;a &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;in &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Database.Assessments)
    {
        &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;SelectListItem &lt;/span&gt;&lt;span style="background: white; color: black"&gt;item = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;SelectListItem&lt;/span&gt;&lt;span style="background: white; color: black"&gt;();

        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Here's where the format string is used...
        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;item.Text = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Format(&lt;font style="background-color: #ffff00"&gt;format&lt;/font&gt;,
            a.Hospital.FictitiousName, a.StartDate.ToShortDateString(), a.EndDate.ToShortDateString());

        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// This is necessary so that white spaces are respected when rendering the HTML code.
        &lt;/span&gt;&lt;span style="background: white; color: black"&gt;item.Text = item.Text.Replace(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;HttpUtility&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.HtmlDecode(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;&amp;amp;nbsp;&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;));

        item.Value = a.AssessmentId.ToString();

        items.Add(item);
    }

    model.Assessments = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;SelectList&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(items.OrderBy(i =&amp;gt; i.Text), &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;);

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;View(model);
}&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;With this code, now the result (really better to spot the dates) is this:&lt;/p&gt;

&lt;p align="center"&gt;&lt;img title="DropDownList with Text aligned using a format string dynamically built" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="DropDownList with Text aligned using a format string dynamically built" src="http://lh4.ggpht.com/-y4hZ6K9HJuY/UC7Pu24eMYI/AAAAAAAACPE/4MmNLcI7U1o/DropdownlistTextRightAlignedAfter%25255B6%25255D.png?imgmax=800" width="437" height="115" /&gt;Figure 2 - DropDownList with Text aligned using a format string dynamically built&lt;/p&gt;

&lt;p&gt;&lt;font size="2" face="Courier New"&gt;maxLength&lt;/font&gt; in this case = 20. This is the length of “Unimed Volta Redonda” (the lengthiest string in the list) in this specific case. The format string uses this value to left align this and all other strings with smaller lengths “equally” adding white spaces to compensate the smaller strings.&lt;/p&gt;

&lt;p&gt;One really import thing to take into consideration when doing what this post proposes is to use a &lt;a href="http://en.wikipedia.org/wiki/Monospaced_font#Use_in_computers" target="_blank"&gt;monospaced font&lt;/a&gt; or fixed-width font, because its letters and characters each occupy the same amount of horizontal space. If you don’t use this kind of font, you won’t get the desired result.&lt;/p&gt;

&lt;p&gt;This is the Razor view code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;div &lt;/span&gt;&lt;span style="background: white; color: red"&gt;id&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;reports&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.LabelFor(m =&amp;gt; m.Assessments)&lt;/span&gt;&lt;span style="background: white; color: red"&gt;&amp;amp;nbsp;
&lt;/span&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.DropDownListFor(m =&amp;gt; m.AssessmentId, Model.Assessments,
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Format(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.SelectValue2, &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Assessment),
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{ data_bind=&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;value: selectedAssessment&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: black"&gt;} )&lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;div&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;So in order to make it work I added this CSS code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: maroon"&gt;#reports select
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: red"&gt;font-family&lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;'Courier New'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; &lt;/span&gt;&lt;span style="background: white; color: #006400"&gt;/* well known monospaced font */
    &lt;/span&gt;&lt;span style="background: white; color: red"&gt;width&lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;auto&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Happy C# dynamic text aligning to everyone!&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=53g8zPFzbwQ:y6n8luQpLPs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=53g8zPFzbwQ:y6n8luQpLPs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=53g8zPFzbwQ:y6n8luQpLPs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=53g8zPFzbwQ:y6n8luQpLPs:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=53g8zPFzbwQ:y6n8luQpLPs:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=53g8zPFzbwQ:y6n8luQpLPs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/53g8zPFzbwQ" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/4986841472940833012?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/4986841472940833012?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/53g8zPFzbwQ/string.format-composite-formatting-with-dynamic-length-aligned-text-in-csharp.html" title="string.Format with dynamic length left aligned text in C#" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-uNeSSqwI_zc/UC7PtkGTarI/AAAAAAAACO8/hZeii_iJBco/s72-c/DropdownlistTextRightAlignedBefore%25255B5%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/08/string.format-composite-formatting-with-dynamic-length-aligned-text-in-csharp.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cNQX0zfCp7ImA9WhJQFkU.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-6334269704534046642</id><published>2012-07-26T16:03:00.001-03:00</published><updated>2012-07-30T17:44:50.384-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-30T17:44:50.384-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="debug" /><category scheme="http://www.blogger.com/atom/ns#" term="web deploy" /><category scheme="http://www.blogger.com/atom/ns#" term="error" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="publish" /><title>Debugging Microsoft Web Deploy Visual Studio publishing errors</title><content type="html">&lt;p&gt;This post is intended for starters with MS Web Deploy as is my case… &lt;img class="wlEmoticon wlEmoticon-shifty" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Shifty" src="http://lh5.ggpht.com/-XoIrbW8peMY/UBGUWX2KW5I/AAAAAAAACNU/YtANzdB3mR4/wlEmoticon-shifty%25255B2%25255D.png?imgmax=800" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2010/09/13/automating-deployment-with-microsoft-web-deploy.aspx" target="_blank"&gt;Microsoft Web Deploy&lt;/a&gt; is so awesome that I think it’s one of the best features one has at their disposal when developing with the .NET Framework and for the Web with ASP.NET MVC for example. It’s so powerful that it can even publish a database.&lt;/p&gt;  &lt;p&gt;Once upon a time (let’s say before 2010) there was no Web Deploy and you had to FTP to the server to deploy the application. This was a tiresome task and led to many errors because one had to be extremely careful of what files needed to be updated, added, removed, etc while installing the application on the server. Backup methods should be in place in case something went wrong. I used to use compressed files (.zip or .rar) to send the app’s files to the server. It was still a big upload in some cases. I had to wait patiently. Once there I had to uncompress the file. Everything needed to be moved into place to make things easier. I even created a step by step readme file to help other peers do the deploy on the server. This readme detailed the workflow that should be followed in order to get things working.&lt;/p&gt;  &lt;p&gt;Thanks God today there’s Web Deploy and it changed things drastically: only new or updated files are uploaded to the server. No more long waiting periods to start using the application. As a bonus you also have the option of removing additional files on the server (files/folders that aren’t used anymore). All this happens automatically (could we say automagically) with the click of a button inside Visual Studio 2010 or 2012. You can also run a .cmd file in the command line. This .cmd file is created as part of the Web Deploy Package publish method that you can choose once inside Visual Studio. There’s so many awesome features (read Preview menu option in &lt;a href="http://www.leniel.net/2012/07/debugging-web-deploy-and-visual-studio.html#Figure4"&gt;Figure 4&lt;/a&gt; bellow) that they would not fit in this post… To get a full understanding about Web Deploy look at this post: &lt;a href="http://blogs.msdn.com/b/aspnetue/archive/2012/06/12/visual-studio-2012-rc-deployment-documentation-published.aspx" target="_blank"&gt;Visual Studio 2012 RC Deployment Docs&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Well, 2 weeks ago I tried to solve a problem related to Web Deploy. You can read the full history here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stackoverflow.com/q/11479927/114029" target="_blank"&gt;Visual Studio 2012 Web Deploy to Windows Server 2008 R2 with IIS 7 and /msdeploy.axd 404 error&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The above problem wasn’t solved but I can tell you that I learned a LOT about IIS administration during that weekend. As a result it was worth the effort. I think that a clean install of Windows Server 2008 is necessary in that case…&lt;/p&gt;  &lt;p&gt;Today I was facing a different error (again working against Windows Server 2008 R2) and I followed a simple path to solve it. So here are the basic steps you need to follow to detect the problem’s origin:&lt;/p&gt;  &lt;p align="center"&gt;&lt;a name="Figure1"&gt;&lt;img title="Visual Studio 2012 Publish project with Web Deploy and Validate Connection error" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Visual Studio 2012 Publish project with Web Deploy and Validate Connection error" src="http://lh4.ggpht.com/-tNFpwAGe8v0/UBGUYbn9ljI/AAAAAAAACNc/a9SCjmSWg50/VisualStudio2012PublishWebApplicationError%25255B12%25255D.png?imgmax=800" width="625" height="409" /&gt;&lt;/a&gt;Figure 1 - Visual Studio 2012 Publish project with Web Deploy and Validate Connection error&lt;/p&gt;  &lt;p align="left"&gt;As you see when I clicked the Validate Connection button I got a message: &lt;a href="http://learn.iis.net/page.aspx/1023/web-deploy-error-codes/#ERROR_COULD_NOT_CONNECT_TO_REMOTESVC" target="_blank"&gt;ERROR COULD NOT CONNECT TO REMOTESVC&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;If you click the error message, there’s some more info about the error but it doesn’t really tell you what’s causing the problem.&amp;#160; It’s kind of a generic message you get: go check the Web Management Service on your server… I think I don’t need to tell you that it’s started already! &lt;img class="wlEmoticon wlEmoticon-steamingmad" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Steaming mad" src="http://lh6.ggpht.com/-MRKq9hUb04E/UBGYsPlomtI/AAAAAAAACN4/MEas5NvlSWs/wlEmoticon-steamingmad%25255B2%25255D.png?imgmax=800" /&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Figure 2 - Visual Studio 2012 Publish project with detailed ERROR information" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Figure 2 - Visual Studio 2012 Publish project with detailed ERROR information" src="http://lh6.ggpht.com/-4_HzUmDxYvc/UBGUascK05I/AAAAAAAACNk/gEw-IJVZmlI/VisualStudio2012PublishWebApplicationCouldNotConnectToRemoveSvc%25255B10%25255D.png?imgmax=800" width="490" height="243" /&gt;Figure 2 - Visual Studio 2012 Publish project with “detailed” ERROR information&lt;/p&gt;  &lt;p&gt;To really get to the problem, you’ll need to access the server you’re trying to deploy to. Once there go to this folder:&lt;/p&gt;  &lt;p align="left"&gt;&lt;font size="2" face="Courier New"&gt;C:\inetpub\logs\wmsvc\W3SVC1&lt;/font&gt;&lt;/p&gt;  &lt;p align="left"&gt;This is the standard path where Web Management Service logs what’s going on…&lt;/p&gt;  &lt;p align="left"&gt;When I opened the latest .log file I saw this:&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;#Software: Microsoft Internet Information Services 7.5      &lt;br /&gt;#Version: 1.0       &lt;br /&gt;#Date: 2012-07-26 15:50:30       &lt;br /&gt;#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken       &lt;br /&gt;2012-07-26 12:50:30 192.168.100.77 HEAD /msdeploy.axd site=Default%20Web%20Siste 8172 - &lt;font style="background-color: #ffc000"&gt;189.24.83.277&lt;/font&gt; - &lt;font style="background-color: #ffffff"&gt;401&lt;/font&gt; 2 5 1918       &lt;br /&gt;2012-07-26 12:50:33 192.168.100.77 HEAD /msdeploy.axd site=Default%20Web%20Siste 8172 develop &lt;/font&gt;&lt;font style="background-color: #ffc000" size="2" face="Courier New"&gt;189.24.83.277&lt;/font&gt;&lt;font size="2" face="Courier New"&gt; - &lt;font style="background-color: #ffff00"&gt;550&lt;/font&gt; 0 0 2761&lt;/font&gt;&lt;/p&gt;  &lt;p align="left"&gt;As you see highlighted in orange is my developer machine IP address. This let’s me know that my connection to server is working as expected. Port 8172 is not blocked by the firewall, etc.    &lt;br /&gt;Highlighted in yellow is a sc-status = 550. This is the key part while debugging errors related to web deploy on the server. Now it’s far easier to know what’s the problem. It’s only a matter of Googling about the 550 status code. The first post that mentions this status code was this one:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://fzzd.blogspot.com.br/2011/10/status-code-550-when-using-msdeploy.html" target="_blank"&gt;Status code 550 when using Msdeploy&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The guy lost 1 day to find what was going on and it took me only 1 minute to figure out the error I had with his help. He talks about the site’s name displayed in IIS manager. So I went check mine and to my surprise I had a small mistake in the Site/application name field shown in &lt;a href="http://www.leniel.net/2012/07/debugging-web-deploy-and-visual-studio.html#Figure1"&gt;Figure 1&lt;/a&gt;. I typed Default Web Siste instead of only Default Web Site. What a silly mistake!&lt;/p&gt;  &lt;p&gt;This is the kind of error that can bother you more than it should… believe it or not. You can go the wrong way while trying to get things working as expected.&lt;/p&gt;  &lt;p&gt;After correcting this mistyped named I clicked the Validate Connection button again and to my delight and surprise I got this beautiful green check mark:&lt;/p&gt;  &lt;p align="center"&gt;&lt;img title="Visual Studio 2012 Publish project with Web Deploy and successful connection validation" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Visual Studio 2012 Publish project with Web Deploy and successful connection validation" src="http://lh6.ggpht.com/-KZSVSbYY0Po/UBGUb-NlzPI/AAAAAAAACNs/m-SmKCmRiz8/VisualStudio2012PublishWebApplicationOK%25255B10%25255D.png?imgmax=800" width="573" height="522" /&gt;Figure 3 - Visual Studio 2012 Publish project with Web Deploy and successful connection validation&lt;/p&gt;  &lt;p align="left"&gt;Now if you desire select the Preview menu option to glance at what’s going to the server…&lt;/p&gt; &lt;a name="Figure4"&gt;&lt;/a&gt;  &lt;p align="center"&gt;&lt;a name="Figure4"&gt;&lt;img title="Visual Studio 2012 Publish project with Web Deploy and Previewing the changes" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="Visual Studio 2012 Publish project with Web Deploy and Previewing the changes" src="http://lh6.ggpht.com/-l_RKVagab-A/UBG7Zi1Cn7I/AAAAAAAACOU/Orx3MpYadsE/VisualStudio2012PublishWebDeployPreviewTab%25255B6%25255D.png?imgmax=800" width="625" height="667" /&gt;&lt;/a&gt;    &lt;br /&gt;Figure 4 - Visual Studio 2012 Publish project with Web Deploy and Previewing the changes&lt;/p&gt;  &lt;p align="left"&gt;You can select just the files you want to deploy. This is super useful let’s say when you discover a bug in production and want to change something in whatever file to correct that bug. You already have more changes to pull to the server but you just want to correct that bug. With the Preview tab you can do this: select just the file(s) you changed to correct the bug and deselect all the others. Isn’t this awesome? Of course it is!&lt;/p&gt;  &lt;p align="left"&gt;If it’s OK, just hit the Publish button and you’re good to go.&lt;/p&gt;  &lt;p align="left"&gt;I hope this simple post helps you find the root cause of what’s causing errors while you’re trying to deploy your application using MS Deploy.&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;References      &lt;br /&gt;&lt;/strong&gt;&lt;a href="http://www.asp.net/web-forms/tutorials/deployment/configuring-server-environments-for-web-deployment/configuring-a-web-server-for-web-deploy-publishing-%28remote-agent%29" target="_blank"&gt;Configuring a Web Server for Web Deploy Publishing (Remote Agent)&lt;/a&gt; &lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://learn.iis.net/page.aspx/1075/installing-and-configuring-web-deploy/" target="_blank"&gt;Installing and Configuring Web Deploy&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://learn.iis.net/page.aspx/1023/web-deploy-error-codes/" target="_blank"&gt;Web Deploy error codes&lt;/a&gt;&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=QiIGxo3gBSk:n2VshuhZUvo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=QiIGxo3gBSk:n2VshuhZUvo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=QiIGxo3gBSk:n2VshuhZUvo:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=QiIGxo3gBSk:n2VshuhZUvo:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=QiIGxo3gBSk:n2VshuhZUvo:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=QiIGxo3gBSk:n2VshuhZUvo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/QiIGxo3gBSk" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/6334269704534046642?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/6334269704534046642?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/QiIGxo3gBSk/debugging-web-deploy-and-visual-studio.html" title="Debugging Microsoft Web Deploy Visual Studio publishing errors" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-XoIrbW8peMY/UBGUWX2KW5I/AAAAAAAACNU/YtANzdB3mR4/s72-c/wlEmoticon-shifty%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/07/debugging-web-deploy-and-visual-studio.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08GQn0zeyp7ImA9WhJSEE8.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-8772478336081053681</id><published>2012-06-29T11:32:00.001-03:00</published><updated>2012-06-29T23:03:43.383-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-29T23:03:43.383-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="data" /><category scheme="http://www.blogger.com/atom/ns#" term="partial class" /><category scheme="http://www.blogger.com/atom/ns#" term="data annotation" /><category scheme="http://www.blogger.com/atom/ns#" term="Date" /><category scheme="http://www.blogger.com/atom/ns#" term="hidden field" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="jQuery UI" /><category scheme="http://www.blogger.com/atom/ns#" term="server side" /><category scheme="http://www.blogger.com/atom/ns#" term="client side" /><category scheme="http://www.blogger.com/atom/ns#" term="Foolproof" /><category scheme="http://www.blogger.com/atom/ns#" term="NuGet" /><category scheme="http://www.blogger.com/atom/ns#" term="validation" /><category scheme="http://www.blogger.com/atom/ns#" term="jQuery" /><category scheme="http://www.blogger.com/atom/ns#" term="Datepicker" /><title>Validating Date with MVC Foolproof Validation on the client side</title><content type="html">&lt;p align="left"&gt;This is a quick tip for something that I just tried with an ASP.NET MVC app and that worked as expected.&lt;/p&gt;  &lt;p&gt;I use &lt;a href="http://foolproof.codeplex.com/" target="_blank"&gt;MVC Foolproof Validation&lt;/a&gt; to complement the standard validators that come with ASP.NET MVC in the &lt;font size="2" face="Courier New"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations%28v=vs.110%29.aspx" target="_blank"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt;&lt;/font&gt; assembly. Foolproof has some awesome validators like:&lt;/p&gt;  &lt;pre&gt;&lt;font size="2"&gt;[Is]
[EqualTo]
[NotEqualTo]
[GreaterThan]
[LessThan]
[GreaterThanOrEqualTo]
[LessThanOrEqualTo]&lt;br /&gt;[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;You can get it through a NuGet package &lt;a href="http://nuget.org/packages/foolproof" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s say you have a form with a &lt;font size="2" face="Courier New"&gt;DateTime&lt;/font&gt; field called “Show Until” that is part of a &lt;span style="background: white; color: #2b91af"&gt;&lt;font size="2" face="Courier New"&gt;Notice&lt;/font&gt;&lt;/span&gt; object. This property stores a date value that should be in the future &lt;em&gt;obviously&lt;/em&gt;. It makes no sense to set a date in the past for this field. So the question is: how can you get client side validation for this field without the need of going to the server to compare the date the user entered with today’s date?&lt;/p&gt;

&lt;p&gt;This is what this post will show you…&lt;/p&gt;

&lt;p&gt;This is the model property decorated with the respective data annotation &lt;span style="background: white; color: #2b91af"&gt;&lt;font size="2" face="Courier New"&gt;GreaterThanOrEqualTo&lt;/font&gt;&lt;/span&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: black"&gt;[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Required&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(ErrorMessageResourceName = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Required&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, ErrorMessageResourceType = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;typeof&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;&lt;span style="background: white; color: black"&gt;))]
[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Display&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(Name = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;ShowUntil&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, ResourceType = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;typeof&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;&lt;span style="background: white; color: black"&gt;))]
[&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;GreaterThanOrEqualTo&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;Today&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, ErrorMessageResourceName = &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;GreaterThanOrEqualTo&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, ErrorMessageResourceType = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;typeof&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Localization&lt;/span&gt;&lt;span style="background: white; color: black"&gt;))]
&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DateTime &lt;/span&gt;&lt;span style="background: white; color: black"&gt;ShowUntil { &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;get&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;set&lt;/span&gt;&lt;span style="background: white; color: black"&gt;; }&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Pay attention to that &lt;span style="background: white; color: #a31515"&gt;&lt;font size="2" face="Courier New"&gt;&amp;quot;Today&amp;quot;&lt;/font&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, string &lt;/span&gt;value. It tells Foolproof’s validator to compare the &lt;font size="2" face="Courier New"&gt;ShowUntil&lt;/font&gt; property with a property called&lt;font size="2" face="Courier New"&gt; Today&lt;/font&gt; (the dependent property). Wait a moment: the &lt;span style="background: white; color: #2b91af"&gt;&lt;font size="2" face="Courier New"&gt;Notice&lt;/font&gt;&lt;/span&gt; object doesn’t have a property called &lt;font size="2" face="Courier New"&gt;Today&lt;/font&gt; since it’s not necessary. What’s the point? &lt;img class="wlEmoticon wlEmoticon-confusedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Confused smile" src="http://lh5.ggpht.com/-zIkFUXVQHdE/T-3ASL-nbnI/AAAAAAAABz8/Nha3Kg8YhmE/wlEmoticon-confusedsmile%25255B2%25255D.png?imgmax=800" /&gt;&lt;/p&gt;

&lt;p&gt;Now let’s add a &lt;a href="http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.110%29.aspx" target="_blank"&gt;partial class&lt;/a&gt; to extend the &lt;span style="background: white; color: #2b91af"&gt;&lt;font size="2" face="Courier New"&gt;Notice&lt;/font&gt;&lt;/span&gt; class with a property called &lt;span style="background: white; color: black"&gt;&lt;font size="2" face="Courier New"&gt;Today&lt;/font&gt;&lt;/span&gt;. This property returns Today’s date of course.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Notice
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DateTime &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Today
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;get &lt;/span&gt;&lt;span style="background: white; color: black"&gt;{ &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DateTime&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Today; }
    }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Run the app now, enter a date in the past for the &lt;span style="background: white; color: black"&gt;&lt;font size="2" face="Courier New"&gt;ShowUntil&lt;/font&gt;&lt;/span&gt; field and submit the form. You’ll see that a &lt;a href="http://en.wikipedia.org/wiki/Postback#In_web_development" target="_blank"&gt;postback&lt;/a&gt; is made to the server and only after that the validation error message is shown to the user. Foolproof’s validator doesn’t know what’s the value of &lt;font size="2" face="Courier New"&gt;Today&lt;/font&gt; yet since it’s not part of the form data being edited. Remember: this kind of server side validation is not what we want. We want client side validation. We don’t want this data round-trip to the server just to validate one field in the form. So, how to solve this? It’s pretty simple…&lt;/p&gt;

&lt;p&gt;Inside your form view (&lt;font size="2" face="Courier New"&gt;.cshtml&lt;/font&gt; file) since I’m using C# as the programming language and &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx" target="_blank"&gt;Razor&lt;/a&gt; view engine, add this hidden field:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: yellow; color: black"&gt;@&lt;/span&gt;&lt;span style="background: white; color: black"&gt;Html.HiddenFor(model =&amp;gt; model.Today)&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now reload the page and try entering a date in the past like yesterday’s date. You’ll see that the user gets the validation error message instantly. Why? Because now Foolproof’s jQuery validator that is added to page has Today’s date value present in the form’s HTML output (that’s the purpose of the hidden field). If you look at the form/page source code in the browser (right click the browser window and select “View Page Source” in Firefox), you’ll se this value:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;input &lt;/span&gt;&lt;span style="background: white; color: red"&gt;data-val&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;true&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: red"&gt;id&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;Today&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: red"&gt;name&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;Today&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: red"&gt;type&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;hidden&amp;quot; &lt;/span&gt;&lt;span style="background: white; color: red"&gt;value&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;29/06/2012 00:00:00&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;That’s it. No date in the past is allowed when adding a new Notice to the system.&lt;/p&gt;

&lt;p&gt;This use case can be considered a good use for &lt;a href="http://wiki.asp.net/page.aspx/298/hiddenfield/" target="_blank"&gt;Hidden fields&lt;/a&gt;. They serve a multitude of purposes…&lt;/p&gt;

&lt;p&gt;Hope it helps.&lt;/p&gt;

&lt;p align="left"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;Updated at 12:45 PM&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p align="left"&gt;You see how things are... after posting this I just discovered a really &lt;em&gt;straightforward&lt;/em&gt; way of getting what I wanted (by chance) I can tell you. I wasn’t looking for this anymore when I hit this StackOverflow &lt;a href="http://stackoverflow.com/q/4002781/114029" target="_blank"&gt;question&lt;/a&gt;. Looks like God took me there… &lt;img class="wlEmoticon wlEmoticon-openmouthedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Open-mouthed smile" src="http://lh4.ggpht.com/-f4_riW5O4IA/T-3LxwvvvWI/AAAAAAAAB0I/CaGgPzyTUrE/wlEmoticon-openmouthedsmile%25255B2%25255D.png?imgmax=800" /&gt; I happen to be using &lt;a href="http://jqueryui.com/demos/datepicker/" target="_blank"&gt;jQuery UI Datepicker&lt;/a&gt; control too. It has between its vast array of &lt;a href="http://jqueryui.com/demos/datepicker/#options" target="_blank"&gt;options&lt;/a&gt; a &lt;span style="background: white; color: #a31515"&gt;&lt;font size="2" face="Courier New"&gt;minDate&lt;/font&gt;&lt;/span&gt; property. So if you do this:&lt;/p&gt;

&lt;p align="left"&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;script &lt;/span&gt;&lt;span style="background: white; color: red"&gt;type&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;

    &lt;/span&gt;&lt;span style="background: white; color: black"&gt;$(document).ready(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;function &lt;/span&gt;&lt;span style="background: white; color: black"&gt;()
    {
        $(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;&amp;quot;#ShowUntil&amp;quot;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;).datepicker(&lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'option'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: #a31515"&gt;'minDate'&lt;/span&gt;&lt;span style="background: white; color: black"&gt;, &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: black"&gt;Date());
    });

&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="background: white; color: maroon"&gt;script&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p align="left"&gt;&lt;span style="background: white; color: blue"&gt;&lt;font size="2" face="Courier New"&gt;new &lt;/font&gt;&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&lt;font size="2" face="Courier New"&gt;Date()&lt;/font&gt; is Today in Javascript and so the &lt;/span&gt;datepicker won’t allow the selection of dates in the past. One word: AWESOME. One liner with &lt;a href="http://jquery.com/" target="_blank"&gt;jQuery&lt;/a&gt; help!&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6ifyifFs004:OAqazC_wKjs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6ifyifFs004:OAqazC_wKjs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6ifyifFs004:OAqazC_wKjs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6ifyifFs004:OAqazC_wKjs:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=6ifyifFs004:OAqazC_wKjs:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=6ifyifFs004:OAqazC_wKjs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/6ifyifFs004" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/8772478336081053681?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/8772478336081053681?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/6ifyifFs004/jquery-date-validation-foolproof-aspnet.html" title="Validating Date with MVC Foolproof Validation on the client side" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-zIkFUXVQHdE/T-3ASL-nbnI/AAAAAAAABz8/Nha3Kg8YhmE/s72-c/wlEmoticon-confusedsmile%25255B2%25255D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://www.leniel.net/2012/06/jquery-date-validation-foolproof-aspnet.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQHSX48fyp7ImA9WhVaE00.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-1793410070685811488</id><published>2012-06-09T14:13:00.001-03:00</published><updated>2012-06-10T02:32:18.077-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-10T02:32:18.077-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="software" /><category scheme="http://www.blogger.com/atom/ns#" term="job" /><title>Leniel’s Software House (software customizado para seu negócio)</title><content type="html">&lt;p&gt;&lt;iframe height="900" src="https://docs.google.com/viewer?authuser=0&amp;amp;srcid=0B4SVxswDPXtwQjk5VHh4V1ZfQ2M&amp;amp;pid=explorer&amp;amp;a=v&amp;amp;chrome=true&amp;amp;embedded=true" width="620"&gt;&lt;/iframe&gt;&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=7vnA8-MDhpk:Esq9VRamQGY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=7vnA8-MDhpk:Esq9VRamQGY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=7vnA8-MDhpk:Esq9VRamQGY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=7vnA8-MDhpk:Esq9VRamQGY:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=7vnA8-MDhpk:Esq9VRamQGY:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=7vnA8-MDhpk:Esq9VRamQGY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/7vnA8-MDhpk" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1793410070685811488?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/1793410070685811488?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/7vnA8-MDhpk/leniels-software-house.html" title="Leniel’s Software House (software customizado para seu negócio)" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><feedburner:origLink>http://www.leniel.net/2012/06/leniels-software-house.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QBRHoycCp7ImA9WhVbGU4.&quot;"><id>tag:blogger.com,1999:blog-4926735770070291800.post-4807811813713859366</id><published>2012-06-05T19:44:00.001-03:00</published><updated>2012-06-05T20:35:55.498-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-05T20:35:55.498-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dependency" /><category scheme="http://www.blogger.com/atom/ns#" term="resolver" /><category scheme="http://www.blogger.com/atom/ns#" term="IoC" /><category scheme="http://www.blogger.com/atom/ns#" term="filter" /><category scheme="http://www.blogger.com/atom/ns#" term="injection" /><category scheme="http://www.blogger.com/atom/ns#" term="Castle Project" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="Windsor" /><category scheme="http://www.blogger.com/atom/ns#" term="container" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="attribute" /><title>Windsor dependency injection in ASP.NET MVC filter attributes</title><content type="html">&lt;p&gt;I learn so much each day that I don’t have time to post about every interesting thing I discover.&lt;/p&gt;  &lt;p&gt;Generally I get to these discoveries/solutions to overcome a wall that somehow appears while developing a given functionality.&lt;/p&gt;  &lt;p&gt;I use Castle Windsor as my &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank"&gt;Inversion of Control&lt;/a&gt; [ IoC ] container. &lt;a href="http://www.leniel.net/2012/05/user-password-expired-filter-aspnet-mvc.html" target="_blank"&gt;Recently&lt;/a&gt; I’ve been implementing some features that depend heavily on ASP.NET MVC &lt;a href="http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs" target="_blank"&gt;action filter attributes&lt;/a&gt; and at some point I needed to get a reference to a service that hits the database for some data checking rules. Unfortunately one cannot ask a Windsor container to give back a dependency when inside an attribute because as &lt;a href="http://stackoverflow.com/users/13163/krzysztof-kozmic" target="_blank"&gt;Krzysztof Koźmic&lt;/a&gt; (the developer of Windsor) says in &lt;a href="http://stackoverflow.com/a/6131160/114029" target="_blank"&gt;his answer&lt;/a&gt; for the question: &lt;a href="http://stackoverflow.com/q/6126663/114029" target="_blank"&gt;Injecting dependency into CustomAttribute using Castle Windsor&lt;/a&gt; at StackOverflow:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;You can't. Attributes are metadata. Putting behavior into them is wrong. Putting dependencies is even worse.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p align="left"&gt;Although I understand the point I Googled a little bit more and found this great post by &lt;a href="http://www.simple-talk.com/author/dino-esposito/" target="_blank"&gt;Dino Esposito&lt;/a&gt; called &lt;a href="http://www.simple-talk.com/dotnet/.net-framework/asp.net-mvc-resolve-or-inject-that%E2%80%99s-the-issue%E2%80%A6/" target="_blank"&gt;ASP.NET MVC: Resolve or Inject? That’s the Issue…&lt;/a&gt;. In this post Dino shows how to use ASP.NET MVC extension point called &lt;a href="http://stackoverflow.com/a/6131160/114029" target="_blank"&gt;DependencyResolver&lt;/a&gt; that sits here &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver%28v=vs.98%29.aspx" target="_blank"&gt;System.Web.Mvc.DependencyResolver&lt;/a&gt;. It simplifies service location and dependency resolution…&lt;/p&gt;  &lt;p align="left"&gt;With this I was able to do the following in a custom &lt;font size="2" face="Courier New"&gt;WindsorDependencyResolver.cs&lt;/font&gt; class:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;WindsorDependencyResolver &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;IDependencyResolver
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;IWindsorContainer &lt;/span&gt;&lt;span style="background: white; color: black"&gt;_container;

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: black"&gt;WindsorDependencyResolver(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;IWindsorContainer &lt;/span&gt;&lt;span style="background: white; color: black"&gt;container)
    {
        _container = container;
    }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Object &lt;/span&gt;&lt;span style="background: white; color: black"&gt;GetService(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Type &lt;/span&gt;&lt;span style="background: white; color: black"&gt;serviceType)
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;_container.Kernel.HasComponent(serviceType) ? _container.Resolve(serviceType) : &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;null&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
    }

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;public &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;IEnumerable&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;object&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt; GetServices(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;Type &lt;/span&gt;&lt;span style="background: white; color: black"&gt;serviceType)
    {
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;_container.Kernel.HasComponent(serviceType) ? _container.ResolveAll(serviceType).Cast&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;object&lt;/span&gt;&lt;span style="background: white; color: black"&gt;&amp;gt;() : &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new object&lt;/span&gt;&lt;span style="background: white; color: black"&gt;[] { };
    }
}&lt;/span&gt;&lt;/pre&gt;

&lt;p align="left"&gt;The above code I took from this great question at StackOverflow: &lt;a href="http://stackoverflow.com/q/4140860/114029" target="_blank"&gt;Castle Windsor Dependency Resolver for MVC 3&lt;/a&gt;. There’s an extensive list of useful info there.&lt;/p&gt;

&lt;p align="left"&gt;In the container bootstrap code inside &lt;font size="2" face="Courier New"&gt;Global.asax&lt;/font&gt; file we put this class into use:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;private static void &lt;/span&gt;&lt;span style="background: white; color: black"&gt;BootstrapContainer()
{
    _container = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;WindsorContainer&lt;/span&gt;&lt;span style="background: white; color: black"&gt;().Install(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;FromAssembly&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.This());

    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;controllerFactory = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;WindsorControllerFactory&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(_container.Kernel);

    &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;ControllerBuilder&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Current.SetControllerFactory(controllerFactory);

    &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Create and register the resolver
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;resolver = &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;new &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;WindsorDependencyResolver&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(_container);
    &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DependencyResolver&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.SetResolver(resolver);
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;See how we’re passing Windsor’s app container to the Dependency Resolver. This way when we ask the &lt;font size="2" face="Courier New"&gt;DependencyResolver&lt;/font&gt; for a service/repository/whatever it’ll get it from the container and we’ll take advantage of the container’s powerful capabilities. You can read about such awesome capabilities in this series of posts called &lt;a href="http://stw.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx" target="_blank"&gt;Windsor tutorial - ASP.NET MVC 3 application (To be Seen)&lt;/a&gt; written by Windsor’s developer (&lt;a href="http://kozmic.pl" target="_blank"&gt;Krzysztof Koźmic&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Now it’s just a matter of using the Dependency Resolver for our benefit:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: white; color: blue"&gt;public class &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;My&lt;span style="background: white; color: #2b91af"&gt;Authorize&lt;/span&gt;Attribute &lt;/span&gt;&lt;span style="background: white; color: black"&gt;: &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;AuthorizeAttribute
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;{
    &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;protected override bool &lt;/span&gt;&lt;span style="background: white; color: black"&gt;AuthorizeCore(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;HttpContextBase &lt;/span&gt;&lt;span style="background: white; color: black"&gt;httpContext)
    {
        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// Make sure the user is authenticated.
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(httpContext.User.Identity.IsAuthenticated == &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return false&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

        &lt;/span&gt;&lt;span style="background: white; color: green"&gt;// If User has the &amp;quot;SuperUser&amp;quot; role he can surf all the app...
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;if &lt;/span&gt;&lt;span style="background: white; color: black"&gt;(httpContext.User.IsInRole(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;AccountController&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.SuperUser))
            &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return true&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;
&lt;/span&gt;&lt;span style="background: white; color: black"&gt;
        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;var &lt;/span&gt;&lt;span style="background: white; color: black"&gt;permissionService = &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;DependencyResolver&lt;/span&gt;&lt;span style="background: white; color: black"&gt;.Current.GetService(&lt;/span&gt;&lt;span style="background: white; color: blue"&gt;typeof&lt;/span&gt;&lt;span style="background: white; color: black"&gt;(&lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;PermissionService&lt;/span&gt;&lt;span style="background: white; color: black"&gt;)) &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;as &lt;/span&gt;&lt;span style="background: white; color: #2b91af"&gt;PermissionService&lt;/span&gt;&lt;span style="background: white; color: black"&gt;;

        &lt;/span&gt;&lt;span style="background: white; color: blue"&gt;return &lt;/span&gt;&lt;span style="background: white; color: black"&gt;permissionService.UserHasPermission(httpContext);
    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The real good thing about the Dependency Resolver is that once you set the resolver you can call it throughout your ASP.NET MVC…&lt;/p&gt;

&lt;p&gt;OK. It diverges from &lt;a href="http://stackoverflow.com/q/6126663/114029" target="_blank"&gt;Krzysztof’s&lt;/a&gt; main point that one should not put behavior in attributes, but it does the job and is working as expected in my case.&lt;/p&gt;

&lt;p&gt;This has helped me to continue my development and I hope it’ll also help you.&lt;/p&gt;  &lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=BISRP3vxZx8:_zJyWy-JAVA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=BISRP3vxZx8:_zJyWy-JAVA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=BISRP3vxZx8:_zJyWy-JAVA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=BISRP3vxZx8:_zJyWy-JAVA:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:4cEx4HpKnUU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?i=BISRP3vxZx8:_zJyWy-JAVA:4cEx4HpKnUU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/leniel?a=BISRP3vxZx8:_zJyWy-JAVA:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/leniel?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/leniel/~4/BISRP3vxZx8" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/4807811813713859366?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/4926735770070291800/posts/default/4807811813713859366?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/leniel/~3/BISRP3vxZx8/windsor-injection-aspnet-mvc-attributes.html" title="Windsor dependency injection in ASP.NET MVC filter attributes" /><author><name>Leniel Macaferi</name><uri>https://plus.google.com/114767333676393021366</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-iFmNclBdT5Y/AAAAAAAAAAI/AAAAAAAAC08/XrqCp3B0FJU/s512-c/photo.jpg" /></author><feedburner:origLink>http://www.leniel.net/2012/06/windsor-injection-aspnet-mvc-attributes.html</feedburner:origLink></entry></feed>
