How do you access the user agent from ActionFilterAttribute context?

March 27, 2019 1 answer
Asked by m8_clearance
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.

Answers

hiking boot lace
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()
 
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions