UAParser.Core
4.0.3
See the version list below for details.
dotnet add package UAParser.Core --version 4.0.3
NuGet\Install-Package UAParser.Core -Version 4.0.3
<PackageReference Include="UAParser.Core" Version="4.0.3" />
paket add UAParser.Core --version 4.0.3
#r "nuget: UAParser.Core, 4.0.3"
// Install UAParser.Core as a Cake Addin #addin nuget:?package=UAParser.Core&version=4.0.3 // Install UAParser.Core as a Cake Tool #tool nuget:?package=UAParser.Core&version=4.0.3
UserAgent Parser (based on ua_parser C# Library)
This is the ASP.NET Core implementation of ua-parser.
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:
- Søren Enemærke @sorenenemaerke / github
- Atif Aziz @raboof / github
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on UAParser.Core:
Package | Downloads |
---|---|
SapphireLib.Tools
Package Description |
|
YAFNET.Core
YAF.NET Core Library. |
|
Dorbit.Framework
Package Description |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on UAParser.Core:
Repository | Stars |
---|---|
YAFNET/YAFNET
🌐 YAF.NET - C# ASP.NET Forum
|