Hi @Kuler Master,
Here is a whole working demo for how to make a reusable component to display different meta data with targetframework .net 8
in Blazor:
1.Custom Component named CustomMetaTag.razor
@using Microsoft.Extensions.Localization
@inject NavigationManager _navigationManager
@inject IStringLocalizer<CustomMetaTag> _localizer
<HeadContent>
<meta name="description" content="@Description" />
<meta name="keywords" content="@Keywords" />
<meta property="og:url" content="@Url" />
<meta property="og:type" content="website" />
<meta property="og:title" content="@Title" />
<meta property="og:description" content="@OgDescription" />
<meta property="og:image" content="@Image" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@TwitterSite">
<meta name="twitter:creator" content="@TwitterCreator">
<meta name="twitter:title" content="@Title">
<meta name="twitter:description" content="@OgDescription">
<meta name="twitter:image" content="@Image">
</HeadContent>
@code {
[Parameter] public string Description { get; set; }
[Parameter] public string Keywords { get; set; }
[Parameter] public string Url { get; set; }
[Parameter] public string Title { get; set; }
[Parameter] public string OgDescription { get; set; }
[Parameter] public string Image { get; set; }
[Parameter] public string TwitterSite { get; set; } = "@crowd4cash";
[Parameter] public string TwitterCreator { get; set; } = "@crowd4cash";
protected override void OnParametersSet()
{
if (string.IsNullOrEmpty(Url))
{
Url = _navigationManager.Uri;
}
if (string.IsNullOrEmpty(Title))
{
Title = _localizer["PageTitle_Default"];
}
if (string.IsNullOrEmpty(OgDescription))
{
OgDescription = _localizer["PageSubtitle_Default"];
}
}
}
2.Use the component in Home.razor
for example
@page "/"
@using Microsoft.Extensions.Localization
@inject NavigationManager _navigationManager
@inject IStringLocalizer<Home> _localizer
<CustomMetaTag Description="Your page description here"
Keywords="Your, Keywords, Here"
Url="@_navigationManager.Uri"
Title="@_localizer["PageTitle_Home"]"
OgDescription="@_localizer["PageSubtitle_Home"]"
Image="@Path.Combine(_navigationManager.BaseUri, "img/og.png")"
/>
<h1>@_localizer["PageTitle_Home"]</h1>
<p>@_localizer["PageSubtitle_Home"]</p>
3.Add Localization Strings to Resource Files.
4.Configure the localization in Blazor Server
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddHttpClient();
//add this...
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
//add this... app.UseRequestLocalization(app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(Counter).Assembly);
app.Run();
}
}
5.Configure the localization in Blazor WebAssembly
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); //add this...
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena