Browser detection capabilities for asp.net core.
This library does
- Browser detection
- Device type detection
- Operating System detection
.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.
First Header | 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 inside the ConfigureServices
method of Startup.cs
.
public void ConfigureServices(IServiceCollection services)
{
// Add browser detection service
services.AddBrowserDetection();
services.AddMvc();
}
Step 3: Inject IBrowserDetector
to your controller class or view file or middleware and access the Browser
property.
Example usage in controller code
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();
}
}
Example usage in view code
@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>
Example usage in custom middlware
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);
}
}
}
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.
You can further help the project by visiting http://bit.ly/detectbrowser in your browser and see the detection works. File an issue if you see wrong data.