writing an asp.net mvc view engine
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 PresentationTRANSCRIPT
![Page 1: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/1.jpg)
Writing an ASP.NET MVC View Engine
Louis DeJardinPrincipal SDEMicrosoft
DEV345
![Page 2: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/5.jpg)
The entire API: on one slide
ViewEngines.EnginesAdd
IViewEngineFindPartialViewFindViewReleaseView
IViewRender
![Page 6: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/6.jpg)
demo
A “Claymore” View Engine
What’s the least you need to output html?
![Page 7: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/8.jpg)
Return IView when useCache==false
![Page 9: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/9.jpg)
Hit useCache==true only on 2nd+ calls
![Page 10: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/10.jpg)
Never jump the gun
![Page 11: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/11.jpg)
demo
Following the rules
What do you need to follow the rules?
![Page 12: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/13.jpg)
demo
Getting views from files
Rendering markdown from .md files
![Page 14: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/15.jpg)
demo
Bulking out the base View class
![Page 16: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/17.jpg)
demo
Parsing, generating and compiling
![Page 18: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/21.jpg)
Complete an evaluation on CommNet and enter to win!
![Page 22: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/22.jpg)
Scan the Tag to evaluate this session now on myTech•Ed Mobile
![Page 23: Writing an ASP.NET MVC View Engine](https://reader036.vdocuments.us/reader036/viewer/2022062502/56815114550346895dbf32f5/html5/thumbnails/23.jpg)