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

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);

Leave a Comment