C# HttpRequest 与 HttpRequestMessage 与 HttpRequestBase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15718468/
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
HttpRequest vs HttpRequestMessage vs HttpRequestBase
提问by Mehmet Ata?
What are differences between these classes in ASP.NET? As I discovered there is no inheritance relationship between these classes.
ASP.NET 中的这些类之间有什么区别?我发现这些类之间没有继承关系。
Below code returns an instance of HttpRequestWrapper
which is a
HttpRequestBase
and has a
HttpRequest
下面的代码返回一个实例,HttpRequestWrapper
其中is a
HttpRequestBase
和has a
HttpRequest
HttpRequestMessage request = ...;
HttpRequestBase reqBase = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request;
// do somthing with reqBase.Cookies
It seems like Microsoft wanted to annoy us while reaching cookies from HttpRequestMessage.
微软似乎想在从 HttpRequestMessage 获取 cookie 时惹恼我们。
Is it guaranteed that request.Properties["MS_HttpContext"]
will never be null?
是否保证request.Properties["MS_HttpContext"]
永远不会为空?
Or think that an ajax request is handled in an action of ApiController. I can reach IP of the client with two different ways.
或者认为一个ajax请求是在ApiController的一个action中处理的。我可以通过两种不同的方式访问客户端的 IP。
var ip = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request.UserHostAddress
var ip = HttpContext.Current.Request.UserHostAddress
What is the difference between these two?
这两者有什么区别?
Or in general, I can access same request/response data such as Cookie, Header, Requestor Info etc. in different ways. When to use which? Can we say something like "if it is an ajax request, HttpRequest is not guaranteed to work properly because of lack of something so for ajax requests we should use HttpRequestMessage instead"?
或者一般来说,我可以以不同的方式访问相同的请求/响应数据,例如 Cookie、标头、请求者信息等。什么时候用哪个?我们可以说“如果它是一个 ajax 请求,HttpRequest 不能保证正常工作,因为缺少某些东西,所以对于 ajax 请求,我们应该使用 HttpRequestMessage 代替”?
采纳答案by Filip
HttpRequestis present on the Pageand UserControlclasses as a GET-only property. Similarly, most of its own properties are also GET-only (see 1). This class is used by ASP.NET pages to get information about the incoming http request, e.g. read the client IP, cookies, the query string, whatnot. Importantly, it is part of the "Old" System.Web assembly, which has been around since .NET 1.1
HttpRequest作为 GET-only 属性存在于Page和UserControl类中。同样,它自己的大部分属性也是 GET-only(参见1)。ASP.NET 页面使用此类来获取有关传入 http 请求的信息,例如读取客户端 IP、cookie、查询字符串等。重要的是,它是自 .NET 1.1 以来一直存在的“旧”System.Web 程序集的一部分
HttpRequestMessage, on the other hand, is new in .NET 4.5. It is part of System.Net. It can be used both by clients and services to create, send and receive requests and responses over HTTP. It replaces HttpWebRequest, which is obsolete in .NET 4.5
另一方面,HttpRequestMessage是 .NET 4.5 中的新内容。它是System.Net 的一部分。客户端和服务都可以使用它来通过 HTTP 创建、发送和接收请求和响应。它取代了HttpWebRequest,后者在 .NET 4.5 中已过时
On HttpRequestBase and HttpRequestWrapper, best I can do is to just quote the docs
在 HttpRequestBase 和 HttpRequestWrapper 上,我能做的就是引用文档
The HttpRequestWrapper class derives from the HttpRequestBase class and serves as a wrapper for the HttpRequest class. This class exposes the functionality of the HttpRequest class and exposes the HttpRequestBase type. The HttpRequestBase class enables you to replace the original implementation of the HttpRequest class in your application with a custom implementation, such as when you perform unit testing outside the ASP.NET pipeline.
HttpRequestWrapper 类派生自 HttpRequestBase 类并充当 HttpRequest 类的包装器。此类公开 HttpRequest 类的功能并公开 HttpRequestBase 类型。HttpRequestBase 类使您能够使用自定义实现替换应用程序中 HttpRequest 类的原始实现,例如在 ASP.NET 管道外执行单元测试时。