将 JS 文件添加到客户端应用程序。
site.js
window.SetBaseHref = function (elementId, hRef) {
var link = document.getElementById(elementId);
if (link !== undefined) {
link.href = hRef;
}
return true;
}
wwwroot/appsettings.json
{
"Configuration": {
"BaseHRef": "/blue"
}
}
在 index.html 中引用它并在 <base> 上创建一个 id。
<base href="~/" id="BlazorHRef" />
.......
<script src="/site.js"></script>
索引页中的演示
@page "/"
@page "/red/"
<h1>Hello, world!</h1>
Welcome to your new app.
<button type="button" @onclick="this.SetBaseHRef">SetHRef</button>
<SurveyPrompt Title="How is Blazor working for you?" />
@code {
[Inject] private IJSRuntime _js { get; set; }
private bool _isAltHRef;
private string _altHRef => _isAltHRef ? "/" : "/red";
public async Task SetBaseHRef()
{
await this.SetBaseHref("BlazorHRef", _altHRef);
_isAltHRef = !_isAltHRef;
}
public ValueTask<bool> SetBaseHref(string elementId, string hRef)
=> _js.InvokeAsync<bool>("SetBaseHref", elementId, hRef);
}
在 App.razor 中设置它在启动时
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@code {
[Inject] IConfiguration Configuration { get; set; }
[Inject] private IJSRuntime _js { get; set; }
protected async override Task OnAfterRenderAsync(bool firstRender)
{
var baseHRef = Configuration.GetValue<string>("Configuration:BaseHRef");
if (!string.IsNullOrEmpty(baseHRef)) await this.SetBaseHref("BlazorHRef", baseHRef);
}
public ValueTask<bool> SetBaseHref(string elementId, string hRef)
=> _js.InvokeAsync<bool>("SetBaseHref", elementId, hRef);
}
您可以以类似的方式更改标题中的大部分内容。