ASP.NET Core Razor
Deep Dive
About Me
Joseph Woodward
.NET Software Engineer at Just Eat
Blog: josephwoodward.co.uk
Twitter: @joe_mighty
GitHub: https://github.com/JosephWoodward
Available online
Slides and code can be found:
https://github.com/JosephWoodward/RazorPlayground
Agenda
View Engines
Spark View Engine (C#)
Bellevue
Razor View Engine
HTML Helpers
HTML Helpers
HTML Helpers
HTML Helpers
HTML Helpers
HTML Helpers
...awkward
What’s changed in
ASP.NET Core?
Tag Helpers
What are Tag Helpers?
What are Tag Helpers?
Existing Tag Helpers
Before:
After:
Let’s dive in
With great power comes great responsibility...
Ben “Uncle Ben” Parker
“
Potential pitfalls
Pitfall #1 - Lack of transparency
This seems very tough to get on-board with. My biggest concern is how do we easily discern between which more obscure attributes are "TagHelper" related vs which ones are part of the HTML spec?
“
Pitfall #1 - Lack of transparency
What’s this doing?
<img src="/profile.jpg" alt="Profile Picture" />
<img src="http://cdn.com/profile.mobile.jpg" alt="Profile Picture" />
?
Potential Solutions
// Force prefix
@tagHelperPrefix helper:
<helper:img src="/profile.jpg" alt="Profile Picture" />
// Opt-out of helper
<!helper:img src="/profile.jpg" alt="Profile Picture" />
// Remove tag helpers
@removeTagHelper *, RazorDeepDive
<img src="/profile.jpg" alt="Profile Picture" />
Pitfall #2 - Tag Helper Collisions
View Components
Breaking up your view
Reusable views in Razor pre .NET Core
Partial Views:
@Html.RenderPartial("Contacts", Model.Contacts)
Child Actions:
@Html.Action("Home", "Contacts")
[ChildActionOnly]�public ActionResult Contacts() {� return PartialView("_Contacts", _service.Contacts);�}
What are View Components?
Dependency Injection
Dependency Injection
@using RazorDeepDive.Services
@inject IUserConfiguration Configuration
<p>Hello @Configuration.Name</p>
When would you want dependency injection in views?
I don’t know
New in ASP.NET Core 2.0
Tag Helper Components
(Tag Helpers via dependency injection)
Tag Helper Components
Razor Pages
Return of WebForms?
Questions?