Like any program it needs to be compiled and executed. You can think of this template as a little program that takes input like your page model and outputs the resulting HTML. Generating CodeĪ Razor template starts life as a string (or file) with intermixed HTML, C# code, and Razor directives. On top of that, I learned most of this through trial-and-error and reverse engineering and make no assurances that this is the canonical way or even a correct way of doing any of this. Even though a lot of the API is surfaced as public, it's been known to break in subtle ways between releases. Before I do, note that Razor is under heavy development (and has been for a while). I'll discuss each phase in more detail below. Turning Razor content from a string, file, or other source into final rendered HTML requires several phases: This is partly out of necessity as Razor has grown to support at least three different dialects (ASP.NET MVC, Razor Pages, and Blazor), but it also makes using Razor for your own purposes easier too. This distinction wasn't always clear, but recently the ASP.NET team has been focusing on separating Razor the language from Razor for ASP.NET MVC. Instead they're shipped with ASP.NET MVC as additional support on top of Razor, which your Razor code can use. For example, helpers like Html.Partial() and page directives like aren't part of the Razor language. An important distinction that I want to make here is that Razor is not the set of HTML helpers and other support functionality that comes along with ASP.NET MVC. In this case, Razor is used to produce HTML documents. Templating languages are designed to make producing output content easier by intermixing raw output with instructions on how to generate additional programmatically-based output. At it's core, Razor is a templating language. In this post we'll take a look at the current bleeding edge of Razor and how you can use it today to enable template rendering in your own application.īefore we start looking at code, let's back up a step and consider what Razor is (and what it isn't). That started to change with ASP.NET Core and the ASP.NET team has slowly started to address this use case. ![]() For a while, these third-party libraries were the only way to easily use Razor outside ASP.NET MVC because using the ASP.NET code directly was too complicated. Over the years there's been a number of projects designed to make using Razor templates from your own code easier.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |