Mark Everard

Hello, I'm Mark – a PhD physicist turned technologist / architect.

Archive for October, 2012

Building a Hybrid MVC / WebForms site with EPiServer CMS6R2

with 4 comments

We have recently delivered an EPiServer CMS6R2 site where some parts of the application were delivered using the ASP.NET MVC framework, and some using the standard WebForm technology. Although this is currently not a supported or recommended way of building an EPiServer site, I thought I’d share some of the solutions we had to put in place to create our monster……..

Hybrid applications

ASP.NET allows you to create hybrid applications that combine all parts of the ASP.NET stack, from WebForms, MVC to Dynamic Data and WCF.  EPiServer itself has been a hybrid application since the release of EPiServer CMS 6. The Dashboard feature and Gadgets use the MVC framework and it is possible to integrate your own MVC plugins.

MVC templates in EPiServer

Despite being a hybrid application, EPiServer 6 does not support delivering CMS managed MVC templates (though EPiServer 7 does have this feature). Joel Abrahamsson went some way to allowing this with his EPiMVC project. Although the project delivered the basic functionality well, it was difficult to implement some of the more advanced EPiServer features, such as XForms.

Why would you do this?

The beauty of the dream vanished, and breathless horror and disgust filled my heart.

Good question. Very good question! Our requirements were to build a community-based application rather than a typical brochure-ware CMS site. There was still need for content managed ‘article’ type pages but the majority of data presented to a user would come from a set of 3rd party SOAP web-services. This data composed only a small part of the wider business domain (which is why we didn’t consider EPiServer Relate as a suitable solution fit).

The community aspects of this project were required to have a ‘modern interface’, i.e. something clean,  accessible and with support for browsers ranging from no javascript to those allowing for partial page updates using AJAX.  This isn’t impossible to achieve with ASP.NET WebForms but it’s architecture increases the complexity significantly. Also – higher complexity means you should consider your UI testing strategy – again something difficult to achieve with WebForms (lookup WebForms MVP if you’re interested in testable WebForms).

Our solution

EPiServer MVC Hybrid Logical separationWell – the title of this piece probably gives this away, but mainly because of the clear separation between the CMS pages and the community application – we decided to build a hybrid, which would utilise EPiServer as the CMS platform to take advantage of it’s multi-lingual / personalisation features, but deliver all of the community based use-cases as MVC views supplemented with EPiServer data.

Largely these two approaches were isolated, however there were some custom pieces we had to extend and override and build to make a coherent whole.

A Route aware UrlRewriteProvider

EPiServer has its own UrlRouting/Rewriting feature that allows friendly extension-less URLs to be resolved to WebForm templates. MVC by default has its own routing system that allows requests to be routed to the correct Controller.

We needed MVC routes to be honoured over EPiServer friendly urls; with the requests that don’t match a route being handled by EPiServer.  This scenario also allows the MVC templates to be viewed in edit mode by creating a simple container at the correct place in the content tree.

Standard MVC applications define a greedy approach to routing requests – that is the RouteTable contains a wildcard approach and would in most cases match. We had to change this implementation so that we defined only the routes that we needed to resolve. Along with this, we created our own implementation of a EPiServer FriendlyUrlRewriter that checked each request to see if it was satisfied by the RouteTable and if so deferred the request to the MVC routing system.

Additionally – we had used the IRouteConstraint interface to allow us to handle EPiServer language segment slugs within the MVC routes. Our custom constraint used the EPiServer LanguageBranch API to ensure that only enabled language slugs were matched by the MVC routing system.

Delivering PageData to the MVC views

EPiServer’s API already provides a rich method for content retrieval. As each of the community MVC pages would only ever have a single instance, passing this data to each controller was relatively straight-forward. We usedthe standard EPiServer pattern of creating a settings-page that defines page references to each page instance we require. This gives simple API access to the correct PageData object.

Reuse of common components

Creating a site using two different display technologies can lead to a duplication of effort when creating consistent/static page elements such as header / footers and navigation. We were keen to avoid this where possible. We used MVC2 and the WebFormViewEngine to create our MVC views. The WebFormViewEngine is built on top of the existing WebForm display technology meaning we could reuse common ASP.NET web controls directly in our MVC views (note this only works for data display – the PostBack mechanism doesn’t work). We also built a mechanism for rendering MVC partial views in a WebForm context following a similar method to this StackOverflow post.

Exposing the EPiServer Resource Provider

We wanted to expose the EPiServer XML resource provider implementation to our MVC views so that we could have a common repository of language resources in use across the site – so we created a few HTML helper methods that allowed us to hook right in.

Should I do this?

This is just a top-level overview of the main pieces we had to think about to use MVC and EPiServer in this way. The take-away point here is to be very sure that there is real value in this approach before building anything like this. For us – it worked (and is still working) really well……………..

Written by mark

October 17th, 2012 at 11:30 am

Posted in ASP.NET,EPiServer,MVC

London EPiServer Meetup Winter 2012

without comments

Nansen's London Office

Nansen's London Office

Its been too long………………..

The winter EPiServer Developer MeetUp for 2012 will be on 8th November.

This time we’ll be kindly hosted by long-term EPiServer partners but newcomers to the UK shores; Nansen.

  • Thurs 8th November 2012, 18.30 –21.00 ish
  • Nansen – 109-111 Farringdon Road, London – (directions)

I’ve yet to confirm the program, but our focus will be around the upcoming release of EPiServer 7, there is plenty to learn about the new customer features (Blocks, New Edit UI, Packaging to name a few) and as for us, alot to discuss around the much improved API and new developer features.

If you want to share any ideas with the group then please just drop me a line. They don’t have to be long talks or even that formal. The most important thing is the sharing (just as  I’m sure your Mother’s always taught you!)

If you’re interested then please sign-up and RSVP using the meetup group below.

Written by mark

October 8th, 2012 at 1:52 pm

Posted in Community,EPiServer