I Like ASP.NET Web Pages–Part I

February 17th, 2011

ASP.NET Web Pages is a new Microsoft technology for web development. But the built-in template is misleading. It lets new / first time user think it is just an ASP with the Razor syntax. But it is much powerful than that. After all, I created a few fun stuffs with it.

Simple MVC

ASP.NET Web Pages’ Razor engine is the foundation of ASP.NET MVC 3. I found it is possible to do a kind of simple MVC w/o using MVC 3, thanks to ASP.NET Web Pages’ built-in magic routing. For a request like http://…/a/b/c/d/e, it routes to /a.cshtml. a.cshtml then can be used as controller. I created the Get and Post attributes for cshtml page methods to make it Sinatra style.

image

The RenderPage function invokes a view and pushes data to the view at the same time.

Fluent Service API (Monad)

Unlike traditional models / services, where the functions usually return object(s) of some class. My service here returns itself instead, so that the operations can be chained. Exception handling, unit of work and transaction are all easier by chaining. This is inspired by the concept of Monad. Does it looks similar to jQuery.

var service = new Service(selector).Op1().Op2().Op3()…..

The only question is how to spit out the object from the operation chain at the end. It could be a get function, a property and etc. I chose to have an index property just for fun (looks like jQuery).

service[0] // this is the object (DTO, data transfer object) to be displayed in the view.

Dynamic DTO

Back to the age of C/C++, the functions and classes require to be defined in separated .h files. In Java/C#, this is not required. Things get simplified. But the compilers still require the classes to be bound to some interfaces in order to link them. Now in the era of .NET 4 dynamic, world is further simplified.

DTO, the objects that services operate on, can be dynamic. My service class has a dynamic object as DTO. When the object was created from the Request Form, all the properties are created on the fly w/o pre-defining classes and interfaces.

image

If passed the dynamic object to the view via then RenderPage function, we can retrieve the data within the view via the Page property.

image

BTW, the Page property of an ASP.NET Web Page is also a dynamic property bag. We can push data and retrieve data like regular properties.e.g. Page.hasError.

After used above tricks in my GitWeb project on Github. I am really happy with ASP.NET Web Pages so far.

Entry Filed under: Programming

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to the comments via RSS Feed


Calendar

February 2012
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
272829  

Most Recent Posts


1