Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get header values in ASP.NET MVC

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?

like image 547
Aaron Avatar asked Oct 01 '10 21:10

Aaron


People also ask

How do I read header values in Web API?

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 .

How do you pass header values in web API using Httpclient?

var token = "MyToken"; client. DefaultRequestHeaders. Authorization = new AuthenticationHeaderValue(tokenType, token); //... other code removed for brevity.

What is a request header?

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.


1 Answers

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.

like image 167
Sheldon Wei Avatar answered Sep 18 '22 13:09

Sheldon Wei