Over a million developers have joined DZone.

Using Brail View Engine with ASP.NET MVC

DZone's Guide to

Using Brail View Engine with ASP.NET MVC

· ·
Free Resource

After one week of staying away from this blog and resting for a short while, now it’s time to get back to regular blogging with a new post! Hopefully I’m going to publish more posts about new topics in the near future and one of these topics is ASP.NET MVC which is also the topic of the current post.

As you may know, ASP.NET MVC (like other implementations of the MVC pattern) applies view engine mechanism as a way to replace the rendering methods of the applications, and while the built-in view engine would be the most commonly used engine with the ASP.NET MVC, community members have started creating alternative engines to use in their MVC applications. At the moment, there are a few implementations all as a part of MVCContrib open source project that are ported from other technologies like Java and MonoRail.

I’m not very positive about the usage of ported material from other technologies, but it’s the only option at the moment, so we have to choose one engine from this finite list of ported engines. Recently I’ve been evaluating all these engines and tried to use them in simple applications. While doing this, I believed that some community members have written good guides about using NHaml or NVelocity in good details but there wasn’t something analogous for Brail (except its online documentation), so this post is being written as such a guide.


Brail is a view engine designed for MonoRail and is a part of Castle project in which you are able to build text-based views for your applications to replace the default view engine with something more relevant for some tasks. Brail is also ported to the ASP.NET MVC and is a part of MVCContrib project available on CodePlex that is regularly updated with the latest ASP.NET MVC previews with a rich set of additional features that are very helpful.

Brail works as an alternative for the built-in ASP.NET view engine that uses .aspx and .master file extensions to store page and master files. Brail uses .brail extension names, and like its original version, it may use .brailjs extension as well but I haven’t tested it yet.

In Brail files you define your master layout or page interface with HTML/XHTML/CSS code and use its syntax to render dynamic data (as I’ll introduce in a moment).

There are some certain steps to get started with Brail for ASP.NET MVC that are listed here:

  • Add required references to your project.
  • Configure your project.
  • Add Brail view engine to the ASP.NET MVC view engines collection.
  • Create your layout and page Brail files with your desire user interface and regarding the syntax of Brail files.

As you see, these steps are pretty simple and easy to follow, and more interestingly, like any other view engine, you can replace the engine without touching anything in your logic (controller) or model because it’s MVC and that is what it’s supposed to be!!

Like ASP.NET MVC default engine you need to care about naming convention and store your view files in folders with the same name as your controller class within Views folder but for Brail you must store your master files in Layouts sub-folder in Views folder.

Having this background, let me show you how to get started with Brail quickly and easily, but before stepping into the main discussion, let me point that you must have the most recent build of MVCContrib project by grabbing your copy at its CodePlex workspace. Binary release package is sufficient for using the features but you can also take a look at the source code for further learning and inspirations.


The first and obvious step is adding some references to your ASP.NET MVC project in order to be able to apply MVCContrib features such as Brail view engine, and this step consists of a general step for all features included in this project as well as one extra step for each specific feature: in general you need to add a reference to MvcContrib assembly in your project, but you may also need to include extra references for the specific feature that you’re going to use. For Brail view engine case, you need to add references to Boo language and MvcContrib.BrailViewEngine assemblies, so all in all you must have these references in your projects:

  • MvcContrib
  • MvcContrib.BrailViewEngine
  • Boo.Lang
  • Boo.Lang.Compiler
  • Boo.Lang.Extensions
  • Boo.Lang.Parser

You can find Boo language assemblies in many sites but the easiest one is in the bin folder available in the root of Samples folder in MvcContrib binary download package.


The second step is, configuring your ASP.NET MVC application to use Brail, and this is possible by some modifications in Web.Config file which would be familiar to any .NET developer. You add a new section definition and the specific section for <Brail /> element.

The following piece of code shows how you can configure your application to use Brail view engine.

type="MvcContrib.BrailViewEngine.BrailConfigurationSection, MvcContrib.BrailViewEngine"/>
saveDirectory="CompiledViews" />


It’s obvious that you can configure Brail in a few ways such as enabling debuggable code and generating/storing compiled scripts on the disk.


The third step is, adding your Brail view engine to ASP.NET MVC view engines list, and it’s a kind of familiar pattern for ASP.NET NET MVC developers to do this in Global.asax file in one line of code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcContrib.ViewFactories;

namespace BrailSample
public class MvcApplication : System.Web.HttpApplication
public static void RegisterRoutes(RouteCollection routes)

new { controller = "Home", action = "Index" }

new { controller = "Home", action = "About" }

// Add the View Engine
ViewEngines.Engines.Add(new BrailViewFactory());

protected void Application_Start()


BrailViewFactory is the view factory that you should add to the list of ASP.NET MVC view engines and that’s it!

Besides, it’s worth mentioning that for my sample I have to build views for the homepage and About page that will be handled by a single HomeController in which I have two action methods that act like any other action method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using BrailSample.Models;

namespace BrailSample.Controllers
public class HomeController : Controller
public ActionResult Index()
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!";
ViewData["Time"] = DateTime.Now;

return View("Index", "Master");

public ActionResult About()
ViewData["Title"] = string.Format("About");

var keyvan = new Person();
keyvan.Name = "Keyvan Nayyeri";
keyvan.Birthday = DateTime.Parse("10/11/1984");
keyvan.Hometown = "Kermanshah, Kurdistan";
keyvan.Degree = "Bachelor of Science";

return View("About", "Master", keyvan);

If you’re curious about Person class, its code is as simple as what you see below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BrailSample.Models
public class Person
public string Name { get; set; }

public DateTime Birthday { get; set; }

public string Hometown { get; set; }

public string Degree { get; set; }


In Brail engine layouts have the same responsibility as masters in the built-in ASP.NET MVC view engine, and they follow the same syntax as other Brail files. Note that you must add your layouts under Layouts folder with .brail extension, and you logically would build your general site layout in these files.

For my sample I have created a basic layout with this code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<link href="${siteRoot}Content/Site.css" rel="stylesheet" type="text/css" />

${Html.ActionLink('About Me','About')}


I’d point that you can refer to view data parameters with ${ParameterName} syntax as I used for ${Title} and Brail has some preserved parameters like siteRoot and childOutput (and some other parameters) that can be used as placeholders for site root URL and the content of child pages respectively.


The final step, as I had stated, is, building your page files with .brail extension. for my sample, I require two files: one for the Index page and another for the About page.

The code for the Index file is simpler because it just extracts two parameters from the view data parameters list and displays them.


Current time is <strong>${Time}</strong/>

But About file does something more and retrieves its data from model object directly.

<h1>About ${viewData.Model.Name}</h1>

Birthday: ${viewData.Model.Birthday.ToString("MMMM dd, yyyy")}<br />
Hometown: ${viewData.Model.Hometown}<br />
Degree: ${viewData.Model.Degree}<br />


Running my sample application, I see the expected output.

You can download the sample code package for this post from here.



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}