Skip to content
ASP.NET Core Detection service components for identifying details about client device, browser, engine, platform, & crawler.
Branch: dev
Clone or download

README.md

ASP.NET Core Detection

ASP.NET Core client web browser detection extension to resolve devices, platforms, engine of the client.

ASP.NET Core Responsive

Build status

Build history

package nuget
Wangkanai.Detection NuGet Badge
Wangkanai.Detection.Device NuGet Badge
Wangkanai.Detection.Browser NuGet Badge
Wangkanai.Detection.Engine NuGet Badge
Wangkanai.Detection.Platform NuGet Badge
Wangkanai.Detection.Crawler NuGet Badge
Wangkanai.Responsive NuGet Badge

Installation

Installation of detection library is now done with a single package reference point.

PM> install-package Wangkanai.Detection -pre

While it is still possible to install the individual package if you just need that specific resolver.

PM> install-package Wangkanai.Detection.Device -pre  
PM> install-package Wangkanai.Detection.Browser -pre  
PM> install-package Wangkanai.Detection.Engine -pre   //concept
PM> install-package Wangkanai.Detection.Platform -pre //concept
PM> install-package Wangkanai.Detection.Crawler -pre  

Installation of Responsive library will bring in all dependency packages (This will include `Wangkanai.Detection.Device).

PM> install-package Wangkanai.Responsive -pre

Configuration

This library host the component to resolve the access client device type.

Implement of the library into your web application is done by configuring the Startup.cs by adding the detection service in the ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
	// Add detection services container and device resolver service.
    services.AddDetection();

    // Add framework services.
    services.AddMvc();
}
  • AddDetection() Adds the detection services to the services container.

While the detection service is configured globally, its can also be configure individually if you only need some functions.

public void ConfigureServices(IServiceCollection services)
{
    // Add detection services container and device resolver service.
    services.AddDetectionCore()
        .AddDevice()
        .AddBrowser()
        .AddPlatform()  // concept
        .AddEngine()    // concept
        .AddCrawler();

    // Add framework services.
    services.AddMvc();
}
  • AddDetectionCore() Adds the detection services to the services container.
  • AddDevice() Adds the device resolver service to the detection core services builder.
  • AddBrowser() Adds the browser resolver service to the detection core services builder.
  • AddPlatform() Adds the platform resolver service to the detection core services builder.
  • AddEngine() Adds the engine resolver service to the detection core services builder.
  • AddCrawler() Adds the crawler resolver service to the detection core services builder.

Responsive is configured in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // Add responsive services.
    services.AddResponsive()
        .AddViewSuffix()
        .AddViewSubfolder();

    // Add framework services.
    services.AddMvc();  
}
  • AddResponsive() Adds the Responsive services to the services container.

  • AddViewSuffix() Adds support for device view files to Suffix. In this sample view Responsive is based on the view file suffix.

    Ex *views/[controller]/[action]/index.mobile.cshtml*

  • AddViewSubfolder() Adds support for device view files to Subfolder. In this sample view Responsive is based on the view file subfolder.

    Ex *views/[controller]/[action]/mobile/index.cshtml*

Configure Middleware

The current device on a request is set in the Responsive middleware. The Responsive middleware is enabled in the Configure method of Startup.cs file.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseResponsive();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Customized able options (beta 3)

This enable the middleware to customized the default response for any type of request device to the configured options.

app.UseResponsive(new ResponsiveOptions
{
    TabletDefault = DeviceType.Mobile
});
  • UseResponsive() Add the responsive middleware into the http pipeline. Its will capture the request and resolve the device to responsive services container.

Global Resolver (beta8)

This library host the component to resolve all the access client related resolver that could resolve.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{
    private readonly IDetection _detection;

    public HomeController(IDetection detection)
    {
        _detection = detection;
    }

    public IActionResult Index()
    {
        return View(_detection);
    }
}
  • IDetection is main service for you to access detection service.

When the Detection is pass to the view you can render results like the following example.

@model Wangkanai.Detection.Detection

<h3>UserAgent</h3>
<code>@Model.UserAgent</code>

<h3>Results</h3>

<table>
    <thead>
        <tr>
            <th>Resolver</th>
            <th>Type</th>
            <th>Version</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>Device</th>
            <td>@Model.Device?.Type.ToString()</td>
            <td></td>
        </tr>
        <tr>
            <th>Browser</th>
            <td>@Model.Browser?.Type.ToString()</td>
            <td>@Model.Browser?.Version</td>
        </tr>
        <tr>
            <th>Platform</th>
            <td>@Model.Platform?.Type.ToString()</td>
            <td>@Model.Platform?.Version</td>
        </tr>
        <tr>
            <th>Engine</th>
            <td>@Model.Engine?.Type.ToString()</td>
            <td>@Model.Engine?.Version</td>
        </tr>
        <tr>
            <th>Crawler</th>
            <td>@Model.Crawler?.Type.ToString()</td>
            <td>@Model.Crawler?.Version</td>
        </tr>
    </tbody>
</table>

Device Resolver

This library host the component to resolve the access client device type.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{    
    private readonly IUserAgent _useragent;
    private readonly IDevice _device;   

    public HomeController(IDeviceResolver deviceResolver)
    {
        _useragent = deviceResolver.UserAgent;
        _device = deviceResolver.Device;
    }

    public IActionResult Index()
    {            
        return View();
    }
}
  • IDetectionService is main service for you to access UserAgent

Browser Resolver (beta7)

This library host the component to resolve the access client browser type and version.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{    
    private readonly IUserAgent _useragent;
    private readonly IBrowser _browser;   

    public HomeController(IBrowserResolver browserResolver)
    {
        _useragent = browserResolver.UserAgent;
        _browser = browserResolver.Browser;
    }

    public IActionResult Index()
    {            
        return View();
    }
}
  • IDetectionService is main service for you to access UserAgent.

Platform Resolver (concept)

This library host the component to resolve the access client platform type and version.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{    
    private readonly IUserAgent _useragent;
    private readonly IPlatform _platform;   

    public HomeController(IPlatformResolver platformResolver)
    {
        _useragent = platformResolver.UserAgent;
        _platform = platformResolver.Platform;
    }

    public IActionResult Index()
    {            
        return View();
    }
}
  • IDetectionService is main service for you to access UserAgent.

Engine Resolver (concept)

This library host the component to resolve the access client engine type and version.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{    
    private readonly IUserAgent _useragent;
    private readonly IEngine _engine;   

    public HomeController(IEngineResolver engineResolver)
    {
        _useragent = engineResolver.UserAgent;
        _engine = engineResolver.Engine;
    }

    public IActionResult Index()
    {            
        return View();
    }
}
  • IDetectionService is main service for you to access UserAgent.

Crawler Resolver (beta8)

This library host the component to resolve the access client crawler type and version.

Example of calling the detection service in the Controller using dependency injection.

public class HomeController : Controller
{    
    private readonly IUserAgent _useragent;
    private readonly ICrawler _crawler;   

    public HomeController(ICrawlerResolver crawlerResolver)
    {
        _useragent = crawlerResolver.UserAgent;
        _crawler = crawlerResolver.Crawler;
    }

    public IActionResult Index()
    {            
        return View();
    }
}
  • IDetectionService is main service for you to access UserAgent.

HttpRequest extensions (beta10) Learn more #1

This would allow quick access to the Detection in any client request.

var browser = Request.Browser();
var device = Request.Device();
var platform = Request.Platform();
var engine = Request.Engine();
var crawler = Request.Crawler();

Directory Structure

  • src - The code of this project lives here
  • test - Unit tests of this project to valid that everything pass specs
  • sample - Contains sample web application of usage

Contributing

All contribution are welcome, please contact the author.

See the LICENSE file.

You can’t perform that action at this time.