Intellisense, Web Extensions & Razor Views in ASP.NET MVC 3


Well, what a fun hour or two I’ve had today tearing out my hair trying to get a DLL of ASP.NET MVC 3 web extensions referenced in the project, with Razor view Intellisense working in a global manner.


The Web Extensions in question are the rather superb Telerik Extensions for ASP.NET MVC.


Basically, the process consists of:


1. Copying the compiled Telerik.Web.Mvc.dll to your projects local bin folder (along with some other script and content files).


2. Referencing the Telerik.Web.Mvc.dll component within your project references.


3. Adding the relevant namespace to the web.config configuration file.


4. Using your newly added extension!


Well, except it’s never quite that easy is it?  First mistake I made was to add the following section of web.config configuration settings:


    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
              Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Telerik.Web.Mvc.UI"/>


to the wrong web.config file!  Yes, this stuff above has to go in the web.config file that sits inside your “Views” folder.  Put this stuff in the main web.config that sits in your “root” folder and it won’t work.


So, having fixed that little problem, I proceeded to the joys of trying to actually use the HTML extension.




Hey, what’s with the red squiggle, Visual Studio?


I’ve added the DLL to the bin folder, referenced said DLL in my project references, and even ensured I’ve added the namespace directive in the correct section of my correct web.config file.  So, why the errror?


Well, it turns out the magic to getting this to work, after you’ve added the web.config bits and referenced the DLL is two-fold:


1) Build your project.


This is required to ensure that Visual Studio is now “aware” of your newly added references, and can rebuild it own internal cache of namespaces, types and methods that will eventually be populated within the Intellisense picker.



2) Make sure you close the View’s code window in the IDE, then re-open it.

This step is the one that got me.  I hadn’t done this, and nowhere (except after a long hand slog of trawling through Google search results) does it mentions that this is needed.  Well, seems it’s a…err.  ahem, feature, of Visual Studio that is required in order to get otherwise working Intellisense to actually work in the currently open file that you’re editing.


That’s it!  That was the problem.  I’d had the view’s code window open all the time within the Visual Studio IDE.  It wasn’t necessarily always the code window with the focus, but it was there within my list of windows tabs.  Closing and re-opening the code window caused the Intellisense to be re-initialised, and all was well.


My hair was saved!

Motivation Motivation Motivation - The 3 M's!

I watched the following video on YouTube the other day:


I found this both fascinating and incredibly insightful.  In thinking about these motivations and how they may relate to myself, I have to say that I agree with the conclusions:  Give your employees Autonomy, Mastery & Purpose and they can do fantastic things!


A very interesting and unexpected insight into what truly motivates us!

Extensible FizzBuzz

I'm sure you all know "FizzBuzz" by now.  It’s a simple test that can be used by interviewers to determine a prospective developer’s ability to code a simple program to “solve” the FizzBuzz problem.  It’s based upon a children’s game, and in the context of its use as a test of a programmer’s ability, it’s been called the software developer’s “Stairway to Heaven”.


For a brief refresher, here’s the “FizzBuzz” test as it relates to software development:

Write a program that iterates through the numbers from 1 to 100.  For all numbers that are multiples of 3, print the word "Fizz”.  For all numbers that are multiples of 5, print the word “Buzz”, and for all numbers that are multiple of both 3 and 5, print the word “FizzBuzz”.  For all other numbers, print the number itself.

“Traditional” solutions to the FizzBuzz test will be something similar to the following C# code:


            for (int i = 1; i <= 100; i++)
                if (i % 3 == 0 && i % 5 == 0)
                else if (i % 3 == 0)
                else if (i % 5 == 0)


There are, of course, various “clever” optimisations that can be applied (such as determining that a number is divisible by both 3 and 5 by checking if it’s divisible by 15 etc.) however, the basic approach, as outlined above, is sufficient to “solve” the FizzBuzz problem.


The funny thing about FizzBuzz is that, upon reading a blog post such as this one, many people absolutely have to rush off and code up their own solution.  There’s nothing wrong with that, but most “solutions” are either wrong, or incredibly trite.  However, every now and then, a truly novel and inspiring solution to the problem comes flying in, right out of leftfield, and really makes you stop, sit up and take notice!


Here is one such solution.  I give you “Extensible FizzBuzz”:


// Extensible FizzBuzz!
Dictionary<Func<int, bool>, Func<int, string>> rules = new Dictionary<Func<int, bool>, Func<int, string>>();
rules.Add(x => x % 3 == 0, x => "fizz");
rules.Add(x => x % 5 == 0, x => "buzz");
rules.Add(x => x % 5 != 0 && x % 3 != 0, x => x.ToString());
rules.Add(x => true, x => "\n");

var output = from n in Enumerable.Range(1, 100)
             from f in rules
             where f.Key(n)
             select f.Value(n);

output.ToList().ForEach(x => Console.Write(x));


This “extensible FizzBuzz” solution uses a Dictionary<T,T> type to contain two Func<T,T> delegates for each dictionary entry.  The TKey of each dictionary entry is a unique lambda expression that represents a “rule” or predicate that can be applied to each int within the “loop” that is eventually created by iterating through all of the numbers created by the Enumerable.Range(1,100) expression.  The lambda expression will return a boolean result depending upon whether the input int matches the rule or not.  The TValue of each dictionary entry is effectively the output that we want to display whenever the int that has been input to our “rule” successfully passes that rule.  Here, the lambda expressions are really as simple as outputting “fizz”, “buzz”, or the int.ToString() itself.  The keen eyed amongst you will have noticed there is even a “rule” (the one defined last) that simply always returns true for whatever input we give it and outputs a “\n” newline control character.


Our output var represents a sequence of elements that “joins” a range of ints from 1 to 100 with the rule-set that we’ve defined.  We then use the result of the “rule” predicate as a where filter and finally select the output from the TValue expression as our actual element value.  Finally, after turning our sequence into a List<> (and thereby forcing the evaluation of all of the sequence’s elements), we iterate over that sequence of elements (using the super-handy .ForEach extension method) and simply output our element’s value to the Console.


Although this novel “rule-based” method of evaluating a given entity is used in the context of the FizzBuzz problem, and in that respect could well be argued to be “over-engineered”, the same method can have practical use in real-world problems.  For example, I was recently developing a system to “grade” or “rank” datasets based upon a range of user-defined “rules”.  This involved extensive and complex SQL queries to perform arbitrary groupings of data.  These groups were then “ranked” based upon a range of user-defined rules, such that groups are deemed high-risk or low-risk based upon such example rules as:  Less than 1 year span between the date stamps for each record within the group or, the same values for a given field occurring more than a certain number of times (where this threshold was itself defined by the user).


Through the use of this kind of “extensible” method for building up queries and clauses, and taking advantage of the wonderful functionality of Expression Trees built right into the .NET framework’s System.Linq.Expressions namespace, made implementing extendable queries and rule-sets a breeze.