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
Get header values in ASP.NET MVC
提问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
回答by Nick Larsen
It should be in the Request.Headers
dictionary.
Request.Headers
字典里应该有。
回答by Robert Dundon
If there is anyone like me, who Googled to see how to get the Content-Type
HTTP request header specifically, it's fairly easy:
如果有像我这样的人,专门通过 Google 搜索查看如何获取Content-Type
HTTP 请求标头,这很容易:
Request.ContentType