I have a requirement to capture the HTTP User Agent header coming in from a device, take the value and remove a 'uuid' This UUID can then be used to direct the device to the correct location to give it the files relevant to the device.
In webforms I was able to get it using
Request.ServerVariables["HTTP_USER_AGENT"]; //inside of Page_Load method
How would I go about this in MVC?
As shown above, the header value can be easily read through the HttpContext object. Please add below generic logic to read through any of the custom headers. HttpContext will be accessible through the WebAPI pipeline and can be available through middleware (as shown in the above example) or .
var token = "MyToken"; client. DefaultRequestHeaders. Authorization = new AuthenticationHeaderValue(tokenType, token); //... other code removed for brevity.
A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.
if in controller, you can easily get the header by this:
Request.Headers.GetValues("XXX");
if the name does not exist, it will throw an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With