Wednesday, 18 February 2009

ASP.NET MVC and the VB XML ViewEngine - Interim 1

This is just as an interim update.

Things have been moving a little slow of late and Part 2 of my journey into ASP.NET MVC and the Spark View Engine has had a minor change of direction. Mainly, I stumbled upon a fantastic new view engine for ASP.NET MVC that utilises the new language feature for VB.NET... Xml Literals.

I just <3 Xml Literals!

Just as a quick example from the standard website that is generated for you when creating a new ASP.NET MVC Project.

Here is the original code for the Index.aspx view...

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


   Home Page



<%= Html.Encode(ViewData("Message")) %>

To learn more about ASP.NET MVC visit http://asp.net/mvc.

Here is the same view using the VB Xml ViewEngine...

Namespace Views.Home
Public Class Index
Inherits SiteMaster

Public Overrides Function Render() As System.Xml.Linq.XElement

  Me.ViewData.Add("Title", "Home")

  Return MyBase.Render()

End Function

Public Overrides Function RenderContent() As XElement
  Return 

<%= Xhtml.Encode(ViewData("Message")) %>

To learn more about ASP.NET MVC visit http://asp.net/mvc.

End Function End Class End Namespace

This is just a very simple example, but it can be very powerful too.

For example, now my views are completely contained with my classes as Xml Literals, the rendered page output is checked at compile time instead of runtime.

I will be explaining more about this new development in the next part of this journey. Until then, note that I have decided to drop my plans to use the Spark View Engine in favour of this new VB.NET Xml Literal based View Engine.

Until then, you can check out this View Engine at it's source, Dmitryr's Blog. There is also an informative interview with Dmitryr at Channel 9.

Enjoy!

No comments:

Post a comment