I need the browser data of the client, so I can evaluate whether the browser is a mobile client for smartphones.
How can I determine the browser data is aspNetCore?
Solution:
The browser information can be read from the request header with the Key: User-Agent.
Code example in Core
String sHeader = Request.Headers["User-Agent"].FirstOrDefault(); ViewData["UserAgent"] = sHeader; |
Display as test:
You can display the current browser information on the footer of a website as in this script
Asp.Net Core MVC C # code in the controller
Here, the header of the client browser is read and passed as ViewData to the view.
public IActionResult Index() { //-< goto View >-- String sHeader = Request.Headers["User-Agent"].FirstOrDefault(); ViewData["UserAgent"] = sHeader;
return View("Index"); //-</ goto View >-- }
|
Razor code in the view
In the display view you can then simply display the ViewData value with Html.Raw (..)
<br /> <span style="display:block"> @Html.Raw(ViewData["UserAgent"]);
</span>
|
In the Chrome browser:
In the Chrome browser you will find the current browser data under the. Network area
To do this, open the developer environment with Alt-Shift-I, go to Network and then refresh the website again.
Requerst View
GET /getuid?https%3A%2F%2Fads.avocet.io%2Fsync%3Finvsrc%3D5575b5a3d6e9d6ab4a43bc78%26ext_uid%3D%24UID HTTP/1.1 Host: secure.adnxs.com Connection: keep-alive User-Agent: Accept: image/webp,image/apng,image/*,*/*;q=0.8 Referer: https://localhost:44390/ Accept-Encoding: gzip, deflate, br Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 Cookie config.js?nwid=1097 getuid?https%3A%2F%2Fads.avocet.io%2Fsync%3Finvsrc%3D5575b5a3d6e9d6ab4a43bc78%26ext_uid%3D%24UID |
Query in Asp.Net Core
Under Asp.Net Core you can read the header data with Headers ["User-Agent"].
It is best to use the Linq.FirstOrDefault () method to overload the values ββonto a string.
The query of the HeaderUserAgent works like this: