Browser detection, device detection and operating system detection capabilities in asp.net core.
License
—
Deps
6
Install Size
—
Vulns
✓ 0
Published
Jan 2, 2024
$ dotnet add package Shyjus.BrowserDetectorBrowser detection capabilities for asp.net core.
This library does
For browser detection in Azure Functions .NET Isolated, check
Shyjus.BrowserDetector.AzureFunctionspackage.
Step 1: Install the BrowserDetector nuget package
Install-Package Shyjus.BrowserDetector
Step 2: Enable the browser detection service by calling AddBrowserDetection method on IServiceCollection in your startup code.
services.AddBrowserDetection();
Step 3: Inject IBrowserDetector to your controller class or view file or middleware and access the Browser property.
public class HomeController : Controller
{
private readonly IBrowserDetector browserDetector;
public HomeController(IBrowserDetector browserDetector)
{
this.browserDetector = browserDetector;
}
public IActionResult Index()
{
var browser = this.browserDetector.Browser;
// Use browser object as needed.
return View();
}
}
@inject Shyjus.BrowserDetection.IBrowserDetector browserDetector
<h2> @browserDetector.Browser.Name </h2>
<h3> @browserDetector.Browser.Version </h3>
<h3> @browserDetector.Browser.OS </h3>
<h3> @browserDetector.Browser.DeviceType </h3>
You can inject the IBrowserDetector to the InvokeAsync method.
public class MyCustomMiddleware
{
private RequestDelegate next;
public MyCustomMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IBrowserDetector browserDetector)
{
var browser = browserDetector.Browser;
if (browser.Type == BrowserType.Edge)
{
await httpContext.Response.WriteAsync("Have you tried the new chromuim based edge ?");
}
else
{
await this.next.Invoke(httpContext);
}
}
}
Name value returned by IBrowser.Name