writing an asp.net mvc view engine

Post on 20-Mar-2016

31 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

DEV345. Writing an ASP.NET MVC View Engine. Louis DeJardin Principal SDE Microsoft. This is a great extensibility point. You can implement a new HTML DSL Existing libraries can become good view engines Built-in and Custom ViewEngines can coexist You can take control of rendering - PowerPoint PPT Presentation

TRANSCRIPT

Writing an ASP.NET MVC View Engine

Louis DeJardinPrincipal SDEMicrosoft

DEV345

This is a great extensibility point

You can implement a new HTML DSLExisting libraries can become good view enginesBuilt-in and Custom ViewEngines can coexist

You can take control of renderingMost ActionResults are View or PartialViewAll Html.Partial calls pass through IViewEngine

You can expose APIs as views@Html.Partial(“AdBlock”, new {Zone=“Banner”})

That said…

This is an advanced topicAssumes familiarity with most MVC conceptsThe nuts and bolts can be boring detailed

Several stable, production-ready options existThis is rarely the simplest approach

Where does this fit in?

MVCController builds ModelModel passed to ViewThe IViewEngine renders the View

Well known view enginesWebForms (.aspx, .ascx)Razor (.cshtml, .vbhtml)Third-party (Spark, NHaml, NVelocity, etc.)

The entire API: on one slide

ViewEngines.EnginesAdd

IViewEngineFindPartialViewFindViewReleaseView

IViewRender

demo

A “Claymore” View Engine

What’s the least you need to output html?

Working well with others

Good news: it’s easy to do rightReturning searchedLocations is vitalThe order of ViewEngine.Engines is significantPay attention to useCache!

Return IView when useCache==false

Hit useCache==true only on 2nd+ calls

Never jump the gun

demo

Following the rules

What do you need to follow the rules?

What View Engines typically do

Typically based on template filesMost have a mix of literal HTML and code

Some have interpreted codeOthers use actual C# compilerSome have an inspired approach to “Literal HTML”

demo

Getting views from files

Rendering markdown from .md files

Things you’ll want on your IView base

Context and ViewDataViewDataDictionaryViewContext and friends

Support for Helpers is greatHtmlHelper HtmlUrlHelper UrlAjaxHelper Ajax

Output methodsWrite and WriteLiteralAuto-Encoding is a Very Good Idea

demo

Bulking out the base View class

Code: bringing templates to life

Need something to control outputOutput expressionsConditional code, loops, etc.

Many different approaches are takenCLR languages via CodeDom or BuildProviderOther syntaxes, compiled or interpreted

This is often the key differentiator

demo

Parsing, generating and compiling

What have we done?

Create a new View EngineTook care of searchedLocations and useCache

Avoiding common problemsProvided an factory _cache

Instantiated from templates from filesTurned an existing template library into a view engine

Parsed and compiled template files

Web Track Resources

http://www.asp.net/http://www.silverlight.net/http://www.microsoft.com/web/gallery/http://www.iis.net/http://weblogs.asp.net/Scottgu/http://www.hanselman.com/blog/

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

http://northamerica.msteched.com

Connect. Share. Discuss.

Complete an evaluation on CommNet and enter to win!

Scan the Tag to evaluate this session now on myTech•Ed Mobile

top related