asp.net-mvc 在 ASP.NET MVC 中获取标头值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3843159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:31:30  来源:igfitidea点击:

Get header values in ASP.NET MVC

asp.net-mvchttp-headers

提问by Aaron

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.

我需要捕获来自设备的 HTTP 用户代理标头,获取值并删除“uuid”,然后可以使用此 UUID 将设备定向到正确的位置,以提供与设备相关的文件。

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? I'm still learning MVC2 so please bear with me :-)

我将如何在 MVC 中解决这个问题?我还在学习 MVC2,所以请耐心等待 :-)

Thanks in advance,

提前致谢,

Aaron

亚伦

回答by Shedom Wei

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.

如果名称不存在,则会抛出异常。

回答by Oded

You do it the same way, in the controller:

你用同样的方式,在控制器中:

Request.ServerVariables.Get("HTTP_USER_AGENT");

The Requestobject is part of ASP.NET, MVC or not.

Request对象是否是 ASP.NET、MVC 的一部分。

See thisfor example.

请参阅示例。

回答by Nick Larsen

It should be in the Request.Headersdictionary.

Request.Headers字典里应该有。

回答by Robert Dundon

If there is anyone like me, who Googled to see how to get the Content-TypeHTTP request header specifically, it's fairly easy:

如果有像我这样的人,专门通过 Google 搜索查看如何获取Content-TypeHTTP 请求标头,这很容易:

Request.ContentType