How do you access the user agent from ActionFilterAttribute context?

By On
I want to try and add some override code that will perform a specific action if the user making the request is using a particular user agent. I have the two following methods that are overridden from the base ActionFilterAttribute  class.
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
public override void OnActionExecuting(HttpActionContext actionContext)
I have seen other tutorials where people are able to access properties of the request objects that I do not seem to be able to access here. This is for a webapi, so I am using System.Web.Http.Filters to implement the filter. The two context objects available in these methods don't seem to be the same. How do I access additional request properties such as the user agent from this method.

1 Answer

- Advertisment -
SEMrush
Answered By Dan On
The user agent is stored as part of the request object headers. You can access it using the following code.
actionContext.Request.Headers.UserAgent.ToString()