Get a user agent in Blazor WebAssembly using JavaScript Interop with the navigator.userAgent property.
[Index.razor]
@page "/"
@inject IJSRuntime JsRuntime
<p>@userAgent</p>
@code {
private string userAgent { get; set; }
protected override async Task OnInitializedAsync()
{
userAgent = await JsRuntime.InvokeAsync<string>("getUserAgent");
}
}
[index.html]
<body>
. . .
. . .
<script>
window.getUserAgent = () => {
return navigator.userAgent;
};
</script>
</body >
Share with