ASP.NET Core client web browser detection extension to resolve devices, platforms, engine of the client.
The library is the base foundation for ASP.NET Core Responsive
package | nuget |
---|---|
Wangkanai.Detection | |
Wangkanai.Detection.Device | |
Wangkanai.Detection.Browser | |
Wangkanai.Detection.Engine | |
Wangkanai.Detection.Platform |
This library host the component to resolve the access client device type, maker, model, and identify if the request is an web crawler.
Installation of device resolver library components.
PM> install-package Wangkanai.Detection.Device -pre
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()
.AddDevice();
// Add framework services.
services.AddMvc();
}
AddDetection()
Adds the detection services to the services container.AddDevice()
Adds the device resolver service to the detection services builder.
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
Installation of device resolver library components.
PM> install-package Wangkanai.Detection.Browser -pre //concept
PM> install-package Wangkanai.Detection.Engine -pre //concept
PM> install-package Wangkanai.Detection.Platform -pre //concept
Configuring the Startup.cs
by adding the Client Service in the ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
// Add browser detection services.
services.AddDetection()
.AddBrowser() // concept
.AddEngine() // concept
.AddPlatform(); // concept
// Add framework services.
services.AddMvc();
}
AddDetection()
Adds the detection services to the services container.AddBrowser()
Adds the browser resolver service to the detection services builder.AddEngine()
Adds the engine resolver service to the detection services builder.AddPlatform()
Adds the platform resolver service to the detection services builder.
Example of calling the client service in the Controller
.
public class HomeController : Controller
{
private readonly IUserAgent _useragent;
private readonly IBrowser _browser;
private readonly IEngine _engine;
private readonly IPlatform _platform;
public HomeController(IBrowserResolver browserResolver,
IEngineResolver engineResolver,
IPlatformResolver platformResolver)
{
_useragent = browserResolver.UserAgent,
_browser = browserResolver.Browser,
_engine = engineResolver.Engine,
_platform = platformResolver.Platform
}
public IActionResult Index()
{
return View();
}
}
(Concept) Add extensions to HttpRequest
Learn more #1
var browser = Request.Browser();
var device = Request.Device();
var platform = Request.Platform();
var engine = Request.Engine();
src
- The code of this project lives heretest
- Unit tests of this project to valid that everything pass specssample
- Contains sample web application of usage
All contribution are welcome, please contact the author.