在Blazor Server应用中解决“hreflang”和“canonical”链接问题,可以按照以下步骤进行操作:
_Imports.razor文件中导入需要的命名空间:@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web
@page指令中添加hreflang和canonical属性:@page "/your-page"
@attribute [Hreflang("en", "https://example.com/your-page")]
@attribute [CanonicalLink("https://example.com/your-page")]
其中,“en”是指当前页面的语言代码,"https://example.com/your-page"是指当前页面的规范化URL。
hreflang:using Microsoft.AspNetCore.Components;
public class HreflangAttribute : Attribute
{
    public string LanguageCode { get; }
    public string Url { get; }
    public HreflangAttribute(string languageCode, string url)
    {
        LanguageCode = languageCode;
        Url = url;
    }
}
canonical链接:using Microsoft.AspNetCore.Components;
public class CanonicalLinkAttribute : Attribute
{
    public string Url { get; }
    public CanonicalLinkAttribute(string url)
    {
        Url = url;
    }
}
head标签中添加hreflang和canonical链接:@using Microsoft.AspNetCore.Components
@inject NavigationManager NavigationManager
@if (HasHreflangAttribute)
{
    
}
@if (HasCanonicalLinkAttribute)
{
    
}
@code {
    [CascadingParameter] private HreflangAttribute HreflangAttribute { get; set; }
    private bool HasHreflangAttribute => HreflangAttribute != null;
    [CascadingParameter] private CanonicalLinkAttribute CanonicalLinkAttribute { get; set; }
    private bool HasCanonicalLinkAttribute => CanonicalLinkAttribute != null;
}
MainLayout.razor组件的head标签中:
    ...
     
hreflang和canonical链接的页面组件中,使用Hreflang和CanonicalLink属性来设置相关信息:@page "/your-page"
@attribute [Hreflang("en", "https://example.com/your-page")]
@attribute [CanonicalLink("https://example.com/your-page")]
以上步骤可以帮助你在Blazor Server应用中解决“hreflang”和“canonical”链接问题,并在PageSpeed Insights中进行测试。请根据你的实际需求进行调整和修改。