.NET framework 4.7 has a
Browser property on
HttpContext.Request which gives you information about the browser, from where the HTTP request came from. Unfortunately, ASP.NET core does not have this feature and the ASP.NET team decided to not port it.
Here is the response from the team.
Hi, the browser caps feature in ASP NET 4.x was a server-based way of doing client feature detection. This approach is generally frowned upon in the “modern” era, where runtime client-side feature detection is preferred, using techniques such as progressive enhancement. For that reason, in addition to being a huge maintenance cost to keep the list even reasonably up-to-date, the feature was not brought forward to ASP NET Core.
While I agree with the above response, IMHO, There are certain use cases where browser detection needs to be done on server side. A good example is a proxy server acting as a gateway for requests coming to your web server. Based on the browser, the gateway can route the traffic to different versions of your app/servers (think about an A/B testing infrastructure, targeting a user base from a specific browser / browser version).
I ended up writing a light weight browser detection library.
This library adds the following capabilities to your asp net core app
- Browser detection
- Device type detection
- Operating system detection
How to use ?
Step 1: Install the BrowserDetector nuget package
Step 2: Enable the browser detection service inside the
ConfigureServices method of
Step 3: Inject
IBrowserDetector to your controller class or view file or middleware and access the
Browser property. The property is lazy loaded.
Example usage in controller code
Example usage in view code
Example usage in custom middlware
You can inject the
IBrowserDetector to the
Help this project ?