Castle Demo AppThe First MonoRail Page

time to read 15 min | 2903 words

So we finally made it to actually talk about MonoRail. :-) Just to set the expectations straight, we are going to create a web site for to the Mythical Bug Tracker application, using MonoRail and Brail. It's going be functional, but it is not going to look pretty. I'm not a designer, and I'm not going to waste my time talking about CSS and color coordination when you can go to someone who actually knows something about them and learn something useful.

In two sentences, what is MonoRail?

MonoRail is a Model - View - Controller framework for ASP.Net applications. It replaces the WebForms model, and simplify development by quite a bit. MonoRail encourages complete seperation of business logic and UI logic. To render the pages, MonoRail uses View Engines, one of those ViewEngines is called NVelocity, and it is a port of the Velocity project from Java. The second View Engine is called Brail and it was written by me, so I think you can guess what we're going to be working with now :-) You can go here for a more objective comparisson of the view engines.

Before we start, we need to create a new class library project called MythicalBugTracker.Web. Add the following references:

  • Castle.MonoRail.Framework.dll
  • Castle.MonoRail.Views.Brail.dll
  • Boo.Lang.dll
  • Boo.Lang.Compiler.dll
  • Boo.Lang.Parser.dll
  • Castle.Components.Common.EmailSender.dll
  • Castle.Components.Common.EmailSender.SmtpEmailSender.dll

Add a web.config file with the following information:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="monoRail"

             type ="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework"/>

  </configSections>

 

  <monoRail>

    <controllers>

      <assembly>MythicalBugTracker.Web</assembly>

    </controllers>

    <viewEngine viewPathRoot="Views"

                 customEngine="Castle.MonoRail.Views.Brail.BooViewEngine, Castle.MonoRail.Views.Brail"/>

  </monoRail>

 

  <system.web>

    <httpHandlers>

      <add verb="*" path="*.rails"

           type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework"/>

    </httpHandlers>

    <httpModules>

      <add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />

    </httpModules>

  </system.web>

 

</configuration>

We define MonoRail, the controllers assembly and the view engine. We also mapped the ".rails" extention to MonoRail. I promise that it will all be much clearer in a couple of minutes, but basically we just tell the ASP.Net runtime to use MonoRail for processing request, and we configure MonoRail with the appropriate parameters.

Now we need to setup the project so we can execute it, this require a bit of tinkering with the project's settings:

  • Go to MythicalBugTracker.Web properties > Build and change the Output Path to "bin\" only.
  • Go to MythicalBugTracker.Web properties > Debug
  • Select "Start External Program" and put "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe" in the text box.
  • In the "Command line arguments", put the this: "/port:8080" "/path:F:\MythicalBugTracker\MythicalBugTracker.Web"

This is just manually configuring the ASP.Net Development Server to run when we run out project, and making sure that the compiled output of the assembly is in the bin directory, where the ASP.Net Development Server expects it. One thing to note if you try to run this on IIS, make sure that you've mapped the ".rails" extention to the asp_net ISAPI filter, otherwise it will not work.

We're nearly done with the setup, now all we need is to create the following directories in our project:

  • Controllers
  • Views

It's a bit of a setup, but there is a MonoRail integration package for VS.Net that makes it all automatic. (I'm working with a newer build than the one the integration was built against, which is why I'm doing everything manually.).

Now we can start writing code, we'll start by create a HomeController class on the Controllers directory, here is the entire class defination:

public class HomeController : Controller

{

    public void Default()

    {

    }

}

Now, create a folder called "Home" in the Views folder, and create a file called "Default.boo" with the following text:

I thought I would get some real content here!

(And they tell me I'm not funny, Huh!)

Press Ctrl+F5 and point your browser to: http://localhost:8080/Home/default.rails

Okay, so we could do it more easily if we were using plain old HTML, but now we know that we have the environment setup correctly, we can move on to slightly more complicated things.

More posts in "Castle Demo App" series:

  1. (03 Mar 2006) ViewComponents, Security, Filters and Friends
  2. (01 Mar 2006) Code Update
  3. (01 Mar 2006) Queries and Foreign Keys
  4. (28 Feb 2006) Complex Interactions
  5. (25 Feb 2006) CRUD Operations on Projects
  6. (22 Feb 2006) Let There Be A New User
  7. (22 Feb 2006) Updating our Users
  8. (20 Feb 2006) Getting serious, the first real page
  9. (20 Feb 2006) MonoRail At A Glance
  10. (20 Feb 2006) The First MonoRail Page
  11. (19 Feb 2006) Many To Many Relations
  12. (19 Feb 2006) Lazy Loading and Scopes
  13. (17 Feb 2006) Active Record Relations
  14. (17 Feb 2006) Getting Started With Active Record