Razor, Routing and Helpers
Razor
• A markup syntax to create dynamic web pages.
• Introduced with ASP.NET MVC 3.
• Embed with .NET languages C# and VB.
<!-- Single statement block --> <!-- Loop -->
@{ var msg = "Hello World"; } @for(int i = 1; i <= 20; i++)
<!-- Inline expression or variable --> {
<p>My message is: @msg</p> <p>Line @i</p>
}
<!– if Statement -->
@if(flag==true) {
<p>Hi</p>
}
Routing
• A pattern matching system that monitor the incoming request
and figure out what to do with that request.
• Matches the incoming request against a set of endpoints and
their criteria. Typically, a way to serve user request.
• Supports Link/URL generation for registered routes.
Request
Routing
Controller Model
Response
View
Request Handling
Routing System
Endpoint1 Url Pattern
Endpoint2 Url Pattern
Endpoint3 Url Pattern
Route Table
Types of Routing
Convention Routing Configuration & Mapping
ASP.NET Core 1.x to 2.x
ASP.NET Core 3.x and above
Attribute Routing Configuration and Mapping
Html Helpers
• These are just like ASP.NET Server controls.
• Extension methods of HtmlHelper class, IHtmlHelper Interface.
• Return Html markup to create form controls.
• You can create your own helpers, or use the built in HTML
helpers.
Html Helpers
• Built-In Html Helper
▪ Standard Html Helpers @Html.TextBox("Name")
▪ Strongly Typed Html Helpers @Html.TextBoxFor(m => m.Name)
▪ Templated Html Helpers @Html.EditorFor(m => m.Name)
• Custom Html Helpers
Url Helper
• Renders HTML links and raw URLs
• Generates links based on routing configuration
• Helps to avoid hard coded Urls
Tag Helpers
• Allows server-side code to embed within HTML elements
in Razor files
• Written in C# and target HTML elements based on
element name, attribute name, or parent tag
• Supports an HTML-friendly development experience
Tag Helpers
• Form Tag Helper • Validation Summary Tag Helper
• Form Action Tag Helper • Partial Tag Helper
• Input Tag Helper • Script Tag Helper
• Label Tag Helper • Link Tag Helper
• Anchor Tag Helper • Cache Tag Helper
• Image Tag Helper • Component Tag Helper
• Select Tag Helper • Distributed Cache Tag Helper
• Textarea Tag Helper • Environment Tag Helper
• Validation Message Tag Helper