Archive for March 2009


ASP.NET MVC, StructureMap, and the 404

March 13th, 2009 — 4:28pm

ASP.NET has a great mechanism for handling 404′s in an elegant way. In a purely declarative manner you can add the following to your Web.config:

<error statusCode="404" redirect="/Session/Error" />

This works great when you are using ASP.NET, but when you’re using StructureMap (or any IoC container) to override the DefaultControllerFactory in ASP.NET MVC you have a bit of a problem in finding the non-existent controller type…namely…an Exception. For example, here’s what I’m doing with the controller factory and StructureMap:

public class InjectionControllerFactory : DefaultControllerFactory {
    protected override IController GetControllerInstance(Type controllerType) {
        return ObjectFactory.GetInstance(controllerType) as Controller;
    }
}

The problem is that when controllerType is null (i.e. there is no controller for the given URL) then an exception is thrown in the call to ObjectFactory.GetInstance. So our fancy error setup in the Web.config is moot because an exception is thrown before we even get there.

So, how do we fix this? Well…it’s simple really…let MVC do it’s thing. The DefaultControllerFactory method that we’re overriding here already handles this situation gracefully, so just call the base method if the ObjectFactory.GetInstance call can’t be completed.

public class InjectionControllerFactory : DefaultControllerFactory {
    protected override IController GetControllerInstance(Type controllerType) {
        if (controllerType != null) {
            return ObjectFactory.GetInstance(controllerType) as Controller;
        return base.GetControllerInstance(controllerType);
    }
}

Voila, now our “virtual” 404 is handled and our special 404 page is displayed.

1 comment » | ASP.NET MVC

ASP.NET MVC En Masse – Areas

March 12th, 2009 — 6:50pm

So I’m embarking on my first really large ASP.NET MVC project and I’m really excited about the prospects. But, with any new programming model comes change and little snags that you didn’t see coming. So I’m going to try and blog about these as the project progresses so others can see how we handled such obstacles and to help reinforce the solution in my own mind.

The first thing that you’ll run into in any large MVC application (and I suspect this would happen with Rails or Django as well) is that you end up with a lot of controllers…I mean…a lot of controllers. Inevitably these controllers revolve around similar functionality but you can’t quite bring yourself to combine them because doing so would create uber-controllers that break single responsibility and become unmanageable in their own right. The solution of course is to inject some conceptual node above the controller layer…in essence you want to group controllers.

So after doing some digging I found an excellent blog post by Phil Haack (the instigator of ASP.NET MVC at Microsoft). I think Phil is great, he’s stuck it to the “man” with an great project that beats WebForms hands down. Anyway, in his post he describes a mechanism for extending routing so that you can add essentially another layer to the application above controller, he calls them “areas” but I don’t think it really matters what you call them

If you download the source code for his prototype you’ll see that it’s basically a standard MVC project with just a few small modifications.

  • AreaRouteHelper.cs – This guy’s job is to add areas to route mapping.  It allows you to map routes inside areas and also map routes to the “root” or controllers that aren’t in an area.
  • AreaViewEngine.cs – this just adds patterns so that aspx/ascx views can be found one more level down in the file structure.
  • Changes to the global.asax to register the new area engine.
  • Changes to how the route table is set up (using AreaRoutHelper methods)

It’s super simple to set up and a really effective way to extend the MVC structure.

Comment » | ASP.NET MVC

iPhone

March 6th, 2009 — 10:24pm

I’ve finally joined the ranks of society…I have an iPhone. I write this to you on that very device. Bravo, Mr. Jobs, bravo.

Comment » | Randomized

Easy “LIKE” Operations in LINQ to SQL

March 6th, 2009 — 11:18am

There are two basic ways to do LIKE operations in LINQ to SQL queries…the easy way and the hard way.  The hard way consists of copious usage of String.StartsWith, String.Contains, and String.EndsWith.  The easy way takes advantage of a handy class called SqlMethods found in the System.Data.Linq.SqlClient namespace.

Using the SqlMethods.Like() method, you can do the following:

var keyword = "Marshmal";
var things = from thing in db.Things
                     where SqlMethods.Like(thing.Name, keyword)
                     select thing;

And if you want to search the start, middle, or end of your string, just tack on the ‘%’ as you would in any normal SQL statement:

var keyword = "mallow";
var things = from thing in db.Things
                     where SqlMethods.Like(thing.Name, "%" + keyword)
                     select thing;

And there you have it…an easy way to find your marshmallows.

Comment » | .NET

Back to top