0

EDIT: I initially only wanted to know how to detect different browsers in Blazor Server-Side and change the behaviour of my app accordingly. The answer to that can be found here: How to use the HttpContext object in server-side Blazor to retrieve information about the user, user agent I then learned about feature detection from @CodeCaster and changed this question, since this seems to be a better solution for my problem.

Here is my initial post:

I want to load different contents on the starting page depending on what the browser that is used on the client side supports. (or alternatively have the client load a certain starting page depending on its browser) Something like this: Browser Detection

What is the best way to detect the browser the client is using and where would be the best location in a Blazor Server-Side App to do so?

dan-kli
  • 568
  • 2
  • 13
  • 1
    Does this answer your question? [Get user-agent and ip in Blazor server-side app](https://stackoverflow.com/questions/59469742/get-user-agent-and-ip-in-blazor-server-side-app) – vernou Dec 01 '21 at 11:58
  • Thanks. Actually an answer to your link recommended this: [How do I get client IP and browser info in Blazor?](https://stackoverflow.com/questions/57982444/how-do-i-get-client-ip-and-browser-info-in-blazor/58610501#58610501) which is exactly what i need – dan-kli Dec 01 '21 at 12:05
  • 1
    No, dont delete it, the answer you got is probably a better one than the ones in the related questions. – Cleptus Dec 01 '21 at 12:17

1 Answers1

1

What is the best way to detect the browser

You don't, that practice from the two past decades needs to go. Use feature detection instead.

See:

Key point (taken from the last link):

The problem with this approach is twofold. First, it bundles multiple assumptions about the features the browser supports in one check. A single wrong assumption can break the site. So as a developer you have to keep track of exactly which features each version of a specific browser supports.

The second issue is that this browser check doesn’t take browser versions into consideration and therefore isn’t future-proof. Even if it works with today’s version of a browser, the next release might not require—or worse, might remove support altogether for—a workaround that the browser detection was used to add to the site.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Okay thanks, I will have a look at it. – dan-kli Dec 01 '21 at 12:07
  • 1
    @hakuna your tatas, This answer is incorrect, and has nothing to do with the question. If you want to accept it, please change your question accordingly. If you still insist that the question is the correct , then the answer is here: https://stackoverflow.com/a/59538319/6152891 – enet Dec 01 '21 at 14:19
  • @enet I'm not saying it's impossible, I'm saying you shouldn't do browser detection. I don't see any proof in your answer that refutes that. – CodeCaster Dec 01 '21 at 14:34
  • @enet I will edit my question – dan-kli Dec 01 '21 at 15:06
  • @CodeCaster, your answer is correct but the question is wrong. That is why I told the asker: If you want to accept it, please change your question accordingly. Otherwise, I think what you say about `Browser Detection` is important, but does not answer how to detect the browser the user is using from Blazor Server App. Sorry, your answer does not do that. – enet Dec 01 '21 at 15:06
  • @enet question: _"I want to load different contents on the starting page depending on what the browser that is used on the client side supports"_. My answer to that is that they shouldn't use browser detection for that, but feature detection. If the question is _"How to get the user agent in a Blazor serverside app"_, then yes, your answer answers that. – CodeCaster Dec 01 '21 at 15:08
  • I changed the question, I hope it fits better now – dan-kli Dec 01 '21 at 15:14