Browser detection capabilities for asp.net core Web API.
This library does
.NET framework 4.7 has Browser
property on HttpContext.Request
which gives you information about the browser, from where there HTTP request came from. Unfortunately, ASP.NET core does not have this. This package can be used for browser & device detection in your ASP.NET core apps.
Name | Operating System | Device type |
---|---|---|
Chrome | Windows | Desktop |
Chrome | Mac OS | Desktop |
Chrome | iOS | Mobile |
Chrome | iOS | Tablet |
Chrome | Android | Mobile |
Chrome | Android | Tablet |
Internet Explorer 11 | Windows | Desktop |
Edge | Windows | Desktop |
Edge | iOS | Tablet |
Edge | iOS | Mobile |
Edge | Android | Mobile |
EdgeChromium | Windows | Desktop |
EdgeChromium | OSX | Desktop |
Opera | Windows | Desktop |
Opera | Mac OS | Desktop |
Opera | iOS | Mobile |
Opera | iOS | Tablet |
Safari | Windows | Desktop |
Safari | Mac OS | Desktop |
Safari | iOS | Mobile |
Safari | iOS | Tablet |
Firefox | Windows | Desktop |
Firefox | Mac OS | Desktop |
Firefox | iOS | Mobile |
Firefox | iOS | Tablet |
If you do not see a specific browser/os/device type combo, please open an issue
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);
}
}
}
IBrowser.Name
I ran benchmarks on Safari and Chrome desktop user agents and those seems to return the results around ~ 1 micro second. Heap allocation varies based on the input.
| Method | Mean |
|--------------- |---------:|
| Chrome_Windows | 1.057 us |
| Safari_Windows | 1.093 us |
1 micro second is one millionth of a second.