GithubHelp home page GithubHelp logo

w8tcha / uap-csharp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ua-parser/uap-csharp

2.0 2.0 1.0 4.96 MB

C# (ASP.NET CORE) implementation of ua-parser

License: Other

C# 73.68% JavaScript 20.05% Perl 1.50% HTML 4.77%
user-agent user-agent-parser useragent useragentparser

uap-csharp's Introduction

UserAgent Parser (based on ua_parser C# Library)

This is the ASP.NET Core implementation of ua-parser.

NuGet

build dotnet

The implementation uses the shared regex patterns and overrides from regexes.yaml (found in uap-core). The assembly embeds the latest regex patterns (enabled through a node module) which are loaded into the default parser. You can create a parser with more updated regex patterns by using the static methods on Parser to pass in specific patterns in yaml format.

Build and Run Tests


You can then build and run the tests by

> dotnet restore UAParser.Core.sln
> dotnet test UAParser.Core.sln

Update the embedded regexes

To pull the latest regexes into the project:

> npm install && grunt

How to use ?

Step 1: Install the UAParser.Core nuget package

> Install-Package UAParser.Core

Step 2: Enable the browser detection service inside the ConfigureServices method of Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    // Add user agent service
    services.AddUserAgentParser();

    services.AddMvc();
}

Step 3: Inject IUserAgentParser to your controller class or view file or middleware and access the ClientInfo property.

Example usage in controller code

public class HomeController : Controller
{
    private readonly IUserAgentParser userAgentParser;
    public HomeController(IUserAgentParser parser)
    {
        this.userAgentParser = parser;
    }
    public IActionResult Index()
    {
        var clientInfo = this.userAgentParser.ClientInfo;
        // Use ClientInfo object as needed.

        return View();
    }
}

Example usage in view code

@inject UAParser.Interfaces.IUserAgentParser parser

<h2> @parser.ClientInfo.Browser.Family </h2>
<h3> @parser.ClientInfo.Browser.Version </h3>
<h3> @parser.ClientInfo.OS.ToString() </h3>
<h3> @parser.ClientInfo.Device.ToString() </h3>

Example usage in custom middlware

You can inject the IUserAgentParser to the InvokeAsync method.

public class MyCustomMiddleware
{
    private RequestDelegate next;
    public MyCustomMiddleware(RequestDelegate next)
    {
        this.next = next;
    }
    public async Task InvokeAsync(HttpContext httpContext, IUserAgentParser parser)
    {
        var clientInfo = parser.ClientInfo;

        if (clientInfo.Browser.Family == "Edge")
        {
            await httpContext.Response.WriteAsync("Have you tried the new chromuim based edge ?");
        }
        else
        {
            await this.next.Invoke(httpContext);
        }
    }
}

Authors:

uap-csharp's People

Contributors

atifaziz avatar chrismckee avatar enemaerke avatar fjardon avatar kevinclement avatar niemyjski avatar sikanderiqbal avatar w8tcha avatar weihanli avatar xafero avatar yuanrui avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

xafero

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.