Substitution Component Samples
The Substitution component emulates the Web Forms asp:Substitution control, which was used to inject dynamic content into output-cached pages. In Blazor, this component invokes a Func<HttpContext, string> callback and renders the returned markup.
Basic Callback
The SubstitutionCallback parameter accepts a function that receives the current HttpContext and returns an HTML string.
<Substitution SubstitutionCallback="@GetTimestamp" />
@code {
private string GetTimestamp(HttpContext? context) =>
$"<em>Rendered at: {DateTime.Now:h:mm:ss tt}</em>";
}
Using HttpContext
The callback receives the current HttpContext, allowing access to request information such as headers, query strings, or the user agent.
<Substitution SubstitutionCallback="@GetRequestInfo" />
@code {
private string GetRequestInfo(HttpContext? context)
{
if (context == null) return "<em>No HttpContext available</em>";
var method = context.Request.Method;
var path = context.Request.Path;
return $"<span>Request: {method} {path}</span>";
}
}
MethodName Property
The MethodName property is preserved for migration reference. In Web Forms, this specified the name of a static callback method. In Blazor, use the SubstitutionCallback parameter instead.
<Substitution MethodName="GetDynamicContent" SubstitutionCallback="@GetDynamicContent" />
Web Forms Equivalent
<!-- Web Forms — used inside output-cached pages -->
<%@ OutputCache Duration="60" VaryByParam="None" %>
<asp:Substitution ID="Sub1" runat="server" MethodName="GetCurrentTime" />
<!-- Code-behind -->
public static string GetCurrentTime(HttpContext context)
{
return DateTime.Now.ToString("h:mm:ss tt");
}