writing an asp.net mvc view engine

23
Writing an ASP.NET MVC View Engine Louis DeJardin Principal SDE Microsoft DEV345

Upload: justis

Post on 20-Mar-2016

40 views

Category:

Documents


0 download

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

Page 1: Writing an ASP.NET MVC View Engine

Writing an ASP.NET MVC View Engine

Louis DeJardinPrincipal SDEMicrosoft

DEV345

Page 2: Writing an ASP.NET MVC View Engine

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 [email protected](“AdBlock”, new {Zone=“Banner”})

Page 3: Writing an ASP.NET MVC View Engine

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

Page 4: Writing an ASP.NET MVC View Engine

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.)

Page 5: Writing an ASP.NET MVC View Engine

The entire API: on one slide

ViewEngines.EnginesAdd

IViewEngineFindPartialViewFindViewReleaseView

IViewRender

Page 6: Writing an ASP.NET MVC View Engine

demo

A “Claymore” View Engine

What’s the least you need to output html?

Page 7: Writing an ASP.NET MVC View Engine

Working well with others

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

Page 8: Writing an ASP.NET MVC View Engine

Return IView when useCache==false

Page 9: Writing an ASP.NET MVC View Engine

Hit useCache==true only on 2nd+ calls

Page 10: Writing an ASP.NET MVC View Engine

Never jump the gun

Page 11: Writing an ASP.NET MVC View Engine

demo

Following the rules

What do you need to follow the rules?

Page 12: Writing an ASP.NET MVC View Engine

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”

Page 13: Writing an ASP.NET MVC View Engine

demo

Getting views from files

Rendering markdown from .md files

Page 14: Writing an ASP.NET MVC View Engine

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

Page 15: Writing an ASP.NET MVC View Engine

demo

Bulking out the base View class

Page 16: Writing an ASP.NET MVC View Engine

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

Page 17: Writing an ASP.NET MVC View Engine

demo

Parsing, generating and compiling

Page 18: Writing an ASP.NET MVC View Engine

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

Page 19: Writing an ASP.NET MVC View Engine

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/

Page 20: Writing an ASP.NET MVC View Engine

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.

Page 21: Writing an ASP.NET MVC View Engine

Complete an evaluation on CommNet and enter to win!

Page 22: Writing an ASP.NET MVC View Engine

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

Page 23: Writing an ASP.NET MVC View Engine