Code Monkey home page Code Monkey logo

browserdetector's Introduction

BrowserDetector

NuGet version (BrowserDetector)

Browser detection capabilities for asp.net core.

This library does

  1. Browser detection
  2. Device type detection
  3. 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.

Browsers supported

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

How to use ?

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);
        }
    }
}

Interpreting the Name values returned by IBrowser.Name

  • Firefox - Firefox browser.
  • EdgeChromium - The new Chromium based Microsoft Edge browser.
  • Edge - The legacy Edge browser.
  • Safari - The Safari browser.
  • Chrome - The Chrome browser.

What is the Perf impact on adding this package ?

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.

Help this project ?

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.

browserdetector's People

Contributors

kshyju avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.