Trying to get the user-agent from request in asp.net web api self host

后端 未结 5 1845
轻奢々
轻奢々 2021-02-05 00:21

I\'m trying to get the user-agent in a web api self host and I\'m either doing it wrong, or the web api itself is altering the user agent string.

I\'ve tried using sever

5条回答
  •  盖世英雄少女心
    2021-02-05 00:39

    The absolutely simplest way to get the full user-agent from inside a WebAPI-controller is by doing this:

    var userAgent = Request.Headers.UserAgent.ToString();
    

    It gives exactly the same result as doing the manual step like this:

    // var headers = request.Headers.GetValues("User-Agent");
    // var userAgent = string.Join(" ", headers);
    

提交回复
热议问题