4

How to get Requested browser name and version in asp.net core 2.0

How to get user Browser name ( user-agent ) in Asp.net Core? returns all the browser installed on client PC. But I need the browser name currently requesting.

enter image description here

4
  • 2
    Possible duplicate of How to get user Browser name ( user-agent ) in Asp.net Core?
    – Krypt1
    Commented May 8, 2018 at 14:08
  • No, the header doesn't return all browsers. It can't - the header is sent by the browser itself. That's the name by which every browser in every OS identifies itself to sites. That string identifies compatibility with existing browsers, the version of the browser's rendering engine and the OS. Commented May 8, 2018 at 14:19
  • 1
    BTW why are you asking? Browser detection is not a good idea if you want to control how a page is rendered. Browsers can lie. All major browsers are evergreen which means they update themselves every month. You can't map browser versions to features or capabilities. That's why all sites use feature detection for the last 5+ years Commented May 8, 2018 at 14:21
  • Headers["User-Agent"] returns 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36." Then which one is currently requesting? Commented May 8, 2018 at 14:32

2 Answers 2

3

Use install-package Wangkanai.Detection -pre and install-package Wangkanai.Detection.Browser -pre in PM to install package. Then... Write the following code in Startup.cs:

services.AddDetection();
services.AddDetectionCore().AddBrowser();

In your Controller:

private readonly IDetection _detection;
        public HomeController(IDetection detection)
        {
            _detection = detection;
        }
public IActionResult Index()
        {
            string browser_info = _detection.Browser.Type.ToString() + _detection.Browser.Version;
            ViewData["a"] = browser_info;
            return View(_detection);
        }
-1

Try this Request.Headers["User-Agent"].ToString()

1
  • 1
    It returns all the browser installed. "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36". But I need the client browser name currently using Commented May 8, 2018 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.