Using KnockoutJS with SignalR in ASP.NET MVC

KnockoutJS is a MVVM implementation for JavaScript written by Steve Sanderson, in my opinion the author of the best ASP.NET MVC textbooks available. Simply put it lets you bind a JavaScript object model to your HTML UI using a Read more

A MongoDB Tutorial using C# and ASP.NET MVC

In this post I'm going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver. MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I Read more

Linq To SQL Tutorial

Check out some of my other Linq to SQL posts: EntityBase Inheritance Modifiers with SQLMetal Linq to SQL with WCF Services Linq to SQL Framework (Repository/Business wrapper) ObjectDataSource binding with server side paging and sorting Load Options Generic Framework using reflection This is a basic tutorial for Read more

Extract content from HTML and split words into an array using C#

Posted on by Joe in C# | 3 Comments

The other day I was messing around with Full Text Search with MongoDB (which I’ll probably write about in the future), and wanted to take some HTML content and remove all the HTML tags, extract the actual content, then split up the words into an array.

I wrote the following extension method that seems to do the trick. I’ve not tested it too much so there are likely some scenarios where it won’t work, but for those I’ve tried it’s worked fine.

using System.Text.RegularExpressions;
using System.Web;

public static string[] Tokenize(this string value)
    //Remove Html tags
    value = Regex.Replace(value, @"<.*?>", string.Empty);

    //Decode Html characters
    value = HttpUtility.HtmlDecode(value);

    //Remove everything but letters, numbers and whitespace characters
    value = Regex.Replace(value, @"[^\w\s]", string.Empty);

    //Remove multiple whitespace characters
    value = Regex.Replace(value, @"\s+", " ");

    //Trim, set to lower case and split to array
    return value.Trim().ToLower().Split(' ');

Model Binding MongoDB ObjectId with ASP.NET MVC

Posted on by Joe in ASP.NET, C#, MongoDB, MVC | 13 Comments

If you’re using the MongoDB C# driver with ASP.NET MVC and have a property of type ObjectId in your model you will get the following error when trying to bind back to the model.

The parameter conversion from type ‘System.String’ to type ‘MongoDB.Bson.ObjectId’ failed because no type converter can convert between these types.

This can easily be resolved by creating a custom Model Binder which I’ll explain how to do in this article.

Read more

Creating strongly typed wrappers around untyped dictionaries in ASP.NET using the Castle DictionaryAdapter

Posted on by Joe in ASP.NET, C#, IoC, MVC | Leave a comment

Often when writing web applications you find yourself writing strongly typed wrapper classes around untyped dictionaries, such as Session, QueryString or even Web.Config Application Settings.

This is quite a tedious task, but using the Castle DictionaryAdapter this is all wrapped up nicely, and all you need to do is create an interface.

Read more

MongoDB windows service won’t start

Posted on by Joe in MongoDB | Leave a comment

A couple of times now my MongoDB windows service won’t start after rebooting my machine. If you look at it in the services list it flicks between Starting and Started states but never really startsm and there doesn’t seem to be a way to get it to stop.

To fix this delete the mongod.lock file in your MongoDB data directory. It will immediately create a new one, but then the service will start.

Editing and binding nested lists with ASP.NET MVC 2

Posted on by Joe in Ajax, ASP.NET, C#, JavaScript, jQuery, MVC | 22 Comments

Dynamically editing lists of data and binding back to the model with MVC is a little complicated as the id’s of the form elements need to all tie up for binding to succeed. Recently I had a model, which contained a list of an object, which in turn contained another nested list. Getting this to easily bind back to the model when adding to the lists dynamically was a bit of a headache so I’ll explain how I did it.

This article is inspired by this article by Steve Sanderson, but I also explain how to adapt it to bind nested lists.

Read more

How to register and get all instances that implement an interface using Autofac

Posted on by Joe in C# | 3 Comments

I’ve just started using Autofac, where I’ve previously used Ninject and StructureMap.

UPDATE (09/06/2001): Although this post is about registering all instances that implement a specific interface, my reason for doing so was to create a bootstrapper task. I’ve recently found AutoFac has an IStartable interface which basically does the same thing.

I have an interface called IBootStrapperTask that looks like this:

public interface IBootStrapperTask
    void Execute();

I then have several tasks that implement this interface, which I want to get and run when my application starts. First I need to register all types that use this interface, which I don’t want to do by automatically registering all types. I can do this like so:

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.Load("MyAssesmbly")).Where(t => typeof(IBootStrapperTask).IsAssignableFrom(t)).InstancePerLifetimeScope().AsImplementedInterfaces();

From what I can tell you can only do this on a per assembly basis. Now I have all my types registered I can get and execute them all:

var container = builder.Build();

var tasks = container.Resolve<IEnumerable<IBootStrapperTask>>();
foreach (var task in tasks)

ASP.NET MVC simple server-side ajax paging using jQuery

Posted on by Joe in Ajax, ASP.NET, C#, jQuery, MVC | 39 Comments

It’s always important to page your data on the server side so that you are only hitting the database to get the page you currently need, rather than getting all the results and paging on the client. In this post I’ll explain how to perform simple ajax paging on the server side using ASP.NET MVC 3.

Read more

Looking for .NET Developers at Community Engine on the Northern Beaches in Sydney

Posted on by Joe in Personal | 6 Comments

I haven’t blogged for a while as I’ve been really busy in my new role as Platform Lead at Community Engine where we are currently looking to expand our team.

We’re doing some awesome stuff here to create a cutting edge product platform that will host our future client websites.  The team is probably the best technically that I’ve worked with, and our implementation of SCRUM is the best I’ve seen. We’re also using all the latest and greatest technologies from Microsoft so it’s a great opportunity to get these skills and get them on your CV!

The product platform is a multi-tenant web application hosted in the cloud with Amazon EC2 written with ASP.NET MVC and .NET 4 and Entity Framework 4.  We’re also making use of MongoDB which is used by the likes of FourSquare and SourceForge, along with Solr for searching and distributed caching with Redis. Who wouldn’t want to work with this technology stack?

If you want to get involved you can apply through our careers page. Make sure you mention my blog when you do. We are also interested in hearing from candidates overseas.

We are looking for a Technical Lead and a number of mid/senior developers, as well as many other roles:

Using the jQuery UI Datepicker with Fancybox

Posted on by Joe in jQuery | 3 Comments

I just wasted what seems to be far too much of my life (about 30 minutes), trying to figure out why the jQuery UI Datapicker wouldn’t work when it was applied to a text box within a Fancybox.

It turns out it was working, but it was being placed behind the Fancybox which had it’s z-index set to 1101.

Adding the following CSS worked a treat

    z-index: 1102 !important;

ASP.NET MVC authorize attribute using action parameters with the ActionFilterAttribute

Posted on by Joe in C#, MVC | 1 Comment

ASP.NET MVC provides the AuthorizeAttribute which ensures there is a logged in user. You can also provide parameters to restrict actions or controllers to only be accessible to certain roles or users. You can also create your own custom authorization attribute derived from AuthorizeAttribute to provide any custom authorization.

In addition to this general authorization you may want to restrict access based on the current user and a parameter from the action. For example, say you have an action method to edit the details of a product.  You would pass the ID of the product to the action method, and you may only want certain users to be able to edit this particular product.  The AuthorizeAttribute doesn’t allow you to do this but you can create your own attribute derived from ActionFilterAttribute which gives you the desired result.

Read more