MVC 3 – Supporting Custom View Engines

 

MVC 3 Preview 1 was released earlier this week. It’s packed with new features such as dependency injection, global filters and new tooling to support multiple view engines, including the new Razor view engine for C# (see the end of this post for a list of related posts). Support for Visual Basic will be included in a future release of MVC. For now, our focus has been on getting as much of the C# support completed, including new project types, item templates and T4 files. In this post I will focus on the new tooling support we included for this release.

Adding Custom View Templates to a Project

We’ve retained the support we introduced in MVC 1 that allow users to customize the T4 templates used by the Add View dialog. You can add a new template for the ASPX (C#) view engine by doing the following:

  1. Create a new MVC 3 (ASPX) project.
  2. In your project, create a new folder named CodeTemplates.
  3. Create a folder named AddView under the CodeTemplates folder.
  4. Create a folder named AspxCSharp under the AddView folder. The name of this folder must match the name of the view engine. In MVC 1 and 2, this folder did not exist since we did not support multiple view engines. If you are converting a project from an earlier version of MVC and you had local T4 files, you will need to add an additional folder and move your files one level deeper.
  5. Place your T4 files in the AspxCSharp folder. Remember to clear the Custom Tool property of the T4 file. Right click on the template, select properties and delete the Custom Tool property.
  6. At this point your solution should look like the example below.

AddViewLocalSolution

You can now use your custom template when adding a view by doing the following:

  1. Build your solution. This ensures that all the types in the solution become available when you want to create a strongly typed view.
  2. Right click on the Views folder, or any of its subfolders and select Add View from the context menu.
  3. Check the Create a strongly-typed view checkbox and select a view data class. This will enable the View content dropdown.
  4. Select your new template.

Untitled

Global Templates

Chances are that if you’ve customized templates in one project, you may want to reuse the templates in other projects. Instead of placing the template within the CodeTemplates folder of a the project you can place your new template alongside the default files that MVC installs under C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddView\AspxCSharp

First-Class Support for Third Party View Engines

Apart from supporting the new Razor view engine, we also added support for other view engines such as Spark and NHaml to register their templates in MVC 3 projects. Although MVC does not ship templates for these view engines, we included some extensibility points to make it easier for developers to benefit from the tooling support we have. The support is still limited and we plan on expanding this in a future release, but we believe that we have a fairly solid foundation to work from right now.

Let’s look at an example on how you might add support for Spark. In this example, I will be making the view engine globally visible, but you can also limit the scope locally within a project.

  1. Create a new folder named SparkViewEngine under C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddView
  2. Place your T4 files for Spark inside the SparkViewEngine folder.
  3. Create an XML file in the SparkViewEngine folder named ViewEngine.xml that contains the following markup:
   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <ViewEngine DisplayName="Spark" 
   3:             ViewFileExtension=".spark" 
   4:             DefaultLayoutPage="~/Views/Shared/Application.spark" 
   5:             PartialViewFileExtension=".spark" />

When you select Add View the dialog will now offer Spark as an option.

Local3

A few things to note about the markup:

  • The DisplayName attribute determines the value in the View engine dropdown. Recall that in step 1 you created a folder named SparkViewEngine. Note that the dropdown simply says Spark, not SparkViewEngine.
  • The ViewFileExtension attribute is required and will be used for normal views, partial views and layout pages.
  • The DefaultLayoutPage attribute is used to specify a default value for the master page textbox.
  • The PartialViewFileExtension attribute is used to specify a different extension if partial views require a different one. If this attribute is not specified then we use the value of the ViewFileExtension attribute.

Beyond Preview 1

The XML for the view engine definition is not 100% supported today. To fully support third party view engines we need to expose additional extensibility points. Below is a list of some areas that we are currently investigating.

  • Users need to be able to hook into the browse functionality to select a different layout (master) page and filter the selection based on the view engine’s supported file extensions.
  • The ContentPlaceHolder ID textbox is specific to ASPX pages. Spark for example, has a similar concept. To ensure that the templating host correctly identifies placeholders we need to enable authors of view engines to provide us with a parser that can extract the content sections from a layout page based on the view engine’s syntax.
  • We haven’t exposed all the attributes in the XML yet, since some of them tie directly into how layout pages are processed and also control the state of certain UI elements.

These are just ideas that we are considering and things may change significantly in the next release. Hopefully the functionality we provided in this preview is enough to get people started and have a better understanding of the direction we are taking with MVC 3 tooling support.

If you are developing a view engine or you wish to integrate into our tooling support, please leave some feedback so that we can start a discussion on what extensibility we should enable.

Enjoy!

Other Resources

Comments

# re: MVC 3 – Supporting Custom View Engines

Monday, August 2, 2010 8:49 AM by Steve Ember

good ~

# re: MVC 3 – Supporting Custom View Engines

Saturday, January 8, 2011 9:06 AM by Robert Greyling

Hey Jacques,

This is great stuff. Any further news on this now that MVC 3 is in RC status? I'm currently ramping up on Spark View Engine tooling to roughly coincide with MVC 3 going RTW.

The parts you describe in this post are fairly rudimentary, but a great start nevertheless. You're right though that more value comes from having full support for things like picking the right content target areas and being able to swap out master layouts on the fly.

Any news on this kind of thing? Also, do you have any advice on deployment? Currently I'm leaning towards something like NuGet deploy, but are there better ways?

All the best,

Rob

# re: MVC 3 – Supporting Custom View Engines

Wednesday, April 27, 2011 1:26 AM by SamSaroia

Will there be an option in future to generate richer view elements? I like technolgoy but it is hard to sell to managers.

# re: MVC 3 – Supporting Custom View Engines

Monday, May 9, 2011 8:56 AM by weblogs.asp.net

Mvc 3 supporting custom view engines.. Huh, really? :)

# re: MVC 3 – Supporting Custom View Engines

Thursday, June 23, 2011 2:44 AM by weblogs.asp.net

Mvc 3 supporting custom view engines.. Nifty :)

# re: MVC 3 – Supporting Custom View Engines

Saturday, September 22, 2012 12:20 PM by SEO Services India

Thanks for sharing, this is a fantastic blog article. Want more.

# re: MVC 3 – Supporting Custom View Engines

Sunday, September 30, 2012 4:45 AM by home

Great blog.Really looking forward to read more. Cool.

# re: MVC 3 – Supporting Custom View Engines

Thursday, October 18, 2012 1:42 PM by crork

ochsFj I am so grateful for your blog article.Really looking forward to read more.

# re: MVC 3 – Supporting Custom View Engines

Friday, October 19, 2012 6:29 AM by adidas

A round of applause for your blog. Really Cool.

# re: MVC 3 – Supporting Custom View Engines

Thursday, October 25, 2012 4:56 AM by justin bieber shirts

I really like and appreciate your post.Thanks Again.

# re: MVC 3 – Supporting Custom View Engines

Saturday, October 27, 2012 6:08 AM by football locks

I truly appreciate this article.Thanks Again. Cool.

# re: MVC 3 – Supporting Custom View Engines

Monday, October 29, 2012 7:28 AM by parfumuri

A round of applause for your blog.Much thanks again. Will read on...

# re: MVC 3 – Supporting Custom View Engines

Monday, October 29, 2012 8:40 AM by character actor

Enjoyed every bit of your blog article. Cool.

# re: MVC 3 – Supporting Custom View Engines

Monday, October 29, 2012 9:14 AM by walt frasier actor

Muchos Gracias for your blog. Keep writing.

# re: MVC 3 – Supporting Custom View Engines

Monday, October 29, 2012 9:24 PM by lose weight without exercise

I truly appreciate this article post. Cool.

# re: MVC 3 – Supporting Custom View Engines

Wednesday, October 31, 2012 8:09 AM by true nutrition reviews

Wow, great blog post. Will read on...

# re: MVC 3 – Supporting Custom View Engines

Friday, November 2, 2012 11:44 AM by haine asos

I am so grateful for your blog post. Really Great.

# re: MVC 3 – Supporting Custom View Engines

Friday, November 2, 2012 12:47 PM by www.used-scales.com

Thanks-a-mundo for the article. Want more.

# re: MVC 3 – Supporting Custom View Engines

Friday, November 2, 2012 1:10 PM by best way to start talking to girls

Enjoyed every bit of your article.Really looking forward to read more. Awesome.

# re: MVC 3 – Supporting Custom View Engines

Sunday, November 4, 2012 7:20 PM by siding installer

Very neat article post.Really looking forward to read more. Great.

# re: MVC 3 – Supporting Custom View Engines

Tuesday, November 6, 2012 11:35 AM by electricity for Cambodia

I really enjoy the post. Want more.

# re: MVC 3 – Supporting Custom View Engines

Wednesday, November 7, 2012 5:43 AM by reviews for shakeology

wow, awesome blog post.Really looking forward to read more. Keep writing.

# re: MVC 3 – Supporting Custom View Engines

Sunday, November 25, 2012 11:54 AM by Supply Chain Certification

Enjoyed every bit of your post.Much thanks again. Cool.

# re: MVC 3 – Supporting Custom View Engines

Monday, April 22, 2013 6:24 AM by Flournoy

Have you ever considered about including a little bit more than just

your articles? I mean, what you say is fundamental and everything.

Nevertheless just imagine if you added some great images

or video clips to give your posts more, "pop"!

Your content is excellent but with images and videos, this site could undeniably be one of the greatest in its field.

Wonderful blog!

# re: MVC 3 – Supporting Custom View Engines

Tuesday, April 23, 2013 10:33 AM by Mccullough

Hi to every body, it's my first pay a visit of this blog; this weblog includes awesome and actually fine material in favor of readers.

# re: MVC 3 – Supporting Custom View Engines

Thursday, April 25, 2013 1:00 AM by Tremblay

After I initially left a comment I seem to have clicked the -Notify me when new

comments are added- checkbox and now each time a comment is added I recieve 4 emails with the exact same comment.

Is there a way you can remove me from that service?

Thanks!

# re: MVC 3 – Supporting Custom View Engines

Thursday, May 2, 2013 10:32 PM by Antonio

Spot on with this write-up, I actually feel this web site needs much more attention.

I'll probably be back again to see more, thanks for the advice!