ASP.NET Core Razor Pages and Markup
Razor is a concise markup language that integrates server-side code (C# or VB) into ASP.NET web pages seamlessly. It simplifies web development by blending code and HTML, enhancing productivity and maintainability.
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<h1>@ViewData["Title"]</h1>
<p>Welcome to my ASP.NET Core Razor Pages!</p>
In this code snippet, the @page directive specifies the page, @model defines the page's model, and @ViewData displays dynamic data within the Razor Page markup.
Resources
- Introduction to Razor Pages in ASP.NET Core | Microsoft Learn
Learn about Razor Pages in ASP.NET Core, including tutorials and documentation. - ASP.NET Web Pages Razor (w3schools.com)
Explore Razor syntax for ASP.NET web pages with examples and explanations.
Inject Configuration
To inject configuration settings in your ASP.NET Core application, use Microsoft.Extensions.Configuration
and inject IConfiguration
into your page or component. Access configuration values using Configuration["KeyName"]
.
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!-- Access configuration value -->
@Configuration["ApplicationInsights:InstrumentationKey"]
Options Pattern
For more structured configuration management, use the options pattern with Microsoft.Extensions.Options
. Inject IOptionsMonitor<TOptions>
with @inject
and access configuration values through OptionsAccessor.Value.PropertyName
.
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<ApplicationInsightsOptions> ApplicationInsightsOptionsAccessor
<!-- Access configuration value -->
var instrumentationKey = ApplicationInsightsOptionsAccessor.Value.InstrumentationKey;
For detailed information, refer to the ASP.NET Core documentation on configuration options.
Comments