Saturday 7 February 2009

ASP.NET MVC and the Spark ViewEngine - Part I

I've finally decided it is time to try out ASP.NET MVC and see if it has any advantages for me over traditional ASP.NET WebForms.

I have only ever create websites using PHP (many moons ago) and ASP.NET WebForms 1.1+.

Over the past number of months, I have been intrigued to try out the new-fangled MVC technology. At the same time, I thought I'd also delve a little in the Spark ViewEngine instead of the built in View Engine. As well as trying out for the first time, Unit Testing with the Visual Studio default test suite, as well as TypeMock. For data access, I will using the latest build of SubSonic 2.1 including a few minor tweaks I've added myself.

I will also be using Visual Studio 2008 as my main development environment and GIT for source control. The development machine is running Windows XP Professional x64 with IIS6.0(ish).

At this moment, ASP.NET MVC is at RC1 and the Spark ViewEngine is currently at version 1.0.317.

As a side note, my main development language of "choice" is VB.NET. Mainly because my work environment encourages its use. A short while ago Roy Osherove was looking for developers who use VB.NET to help test and suggest improvements to VB.NET friendly features for TypeMock which had previously been aimed more towards C#.

It has been a while since I was given the special licence to use TypeMock for testing purposes so I thought now is a good time to fulfil my promises.

This will be the first time that I have actively implemented a proper testing framework including using mocking so please forgive me if my first run at this goes completely haywire.

The Project

In order to try out these new technologies, instead of just putting together lame demos that serve no purpose at all, I will be building a real live application based on a very basic booking application I put together for a friend of mine.

The application as it stands today can be seen at http://www.horsearena.co.nz and is built on ASP.NET WebForms with a SqlServer 2005 backend that is accessed via SubSonic 2.0. It originally usedd NHibernate over (Gods forbid) an MS Access database.

Shamefully, this site is very neglected and is in dire need for an Extreme Makeover.

Therefore I thought it would make an ideal candidate for some MVC love.

Where to Start?

To begin with the first step seems to be to install ASP.NET MVC, Spark and TypeMocks. This is the easy bit. What to do next is the hard bit.

This may be my first project to use all these new tools but it doesn't mean I am completely new to the concepts involved. I have been attempting to keep up to date by reading and listening to as much source material, articles, reviews, tutorials and podcasts as I have time for. This is no easy feat and I am humbled by my colleagues and peers who are able to keep abreast of all these technological changes.

I have been following the concepts behind Test Driven Development and Domain Driven Development etc. and it all looks fine and dandy but I am not yet fully convinced on it's merits or best practices. Basically for this project I will be just winging it a little but leaning towards TDD I imagine. At least my goal is to at least develop some sort of test suite.

Adding a New Project

The next step at least is to add to my solution a new ASP.NET MVC project. This at least creates a nice new project for the new MVC application. At the same time, the project wizard asked if I wanted a test project too so I said, "Yes!"

Screenshot

To the left is the basic project structure that was created. Note that I have made one minor addition to the project. I have included jquery 1.3.1 and the corresponding vsdoc for intellisense.

Technically speaking the vsdoc is for jquery version 1.3 but I am hoping it shouldn't matter to much. In Scott Guthries blog he mentions that the release version for ASP.NET MVC, it will come bundled with version 1.3.1.

This is all fine and dandy but does it work?!

Um... Not exactly :(

Now this is not the first time I started a brand new ASP.NET MVC project though it had been in this particular machine instance.

My first problem I had was the 404 error I was getting.

Why the heck was I getting a 404 error on a brand new web application?

After banging my head against my desk for a moment while checking the properties of the website in IIS and making sure the ".mvc" extension was correctly hooked up, I thought I would check the IIS logs.

Note, there are some handy scripts found in <Program Files>\Microsoft ASP.NET\ASP.NET MVC RC\Scripts to register/unregister the ".mvc" extension for pre-IIS7 and IIS7 classic mode.

Step 1.. Find the logs!

This wasn't quite as hard as I expected at first. It would seem the default location is C:\WINDOWS\system32\LogFiles\W3SVC1\. After quickly scanning the log for today I see the culprit...

Rejected-By-UrlScan

WTF! Oh No! I know what I did!

I was playing around with the Web Platform Installer from Microsoft. A very handy tool for setting up your machine for web development. Why hadn't they come out with this to begin with? It makes it easy to install everything you need, and apparently things you do not necessarily need. In my case, I selected UrlScan 3.1 to be installed. I didn't really need UrlScan. I didn't really understand at the time what UrlScan actually was. I know now. Kinda...

For whatever reason, UrlScan was not liking my web application. Apparently I could configure somehow but I couldn't be bothered so I just uninstalled it.

That didn't work.

Despite uninstalling it was still hooked into IIS. The UrlScan dll was gone from it's installation folder. I tried restarting IIS numerous times. But it refused to let go. I gave up trying to work out how to disable UrlScan and decided it was time for a reboot.

One more thing, because I'm using IIS6 I need to alter the routing in the Global.asax.vb file because I need to specifiy an extension to be mapped for ASP.NET MVC since this application will end up on a shared host where I have no control over the configuration of IIS. As such, the extension I'm going to use is ".aspx".

I replaced the original route...

  routes.MapRoute("Default", _
                  "{controller}/{action}/{id}", _
                  New With {.controller = "Home", .action = "Index", .id = ""})
With the following...
  routes.MapRoute("Default", _
                  "{controller}.aspx/{action}/{id}", _
                  New With {.controller = "Home", .action = "Index", .id = ""})

  routes.MapRoute("Root", _
                  String.Empty, _
                  New With {.controller = "Home", .action = "Index", .id = ""})

Phil Haack has a handy guide for this. Note with ASP.NET MVC RC1 you don't seem to need the fix for the Default.aspx page as it already handles the problem with routing the default page.

Yay! The almost-out-of-the-box ASP.NET MVC application works!

Screenshot

In Part II I begin adding jQuery and the Spark View Engine.

3 comments:

  1. I will be interested to view your progress. You have already got further than me (http://whyror.wordpress.com/2008/11/30/aspnet-building-blocks/)

    ReplyDelete
  2. Hi,

    If you run into issues with Typemock Isolator, let me know. Awaiting feedback.

    Gil Zilberfeld
    Typemock

    ReplyDelete
  3. i hate web platform. i am stuck with urlscan, i cant remove it and it broke everything on my production website :(
    URLscan doesnt show up anywhere in my control panel or otherwise but it does on web platform. its totally screwed.

    ReplyDelete