HttpRequest与HttpRequestMessage与HttpRequestBase [英] HttpRequest vs HttpRequestMessage vs HttpRequestBase

查看:551
本文介绍了HttpRequest与HttpRequestMessage与HttpRequestBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET中这些类之间有什么区别?我发现这些类之间没有继承关系.

What are differences between these classes in ASP.NET? As I discovered there is no inheritance relationship between these classes.

下面的代码返回HttpRequestWrapper的实例,其中is a HttpRequestBasehas a HttpRequest

Below code returns an instance of HttpRequestWrapper which is a HttpRequestBase and has a HttpRequest

HttpRequestMessage request = ...;
HttpRequestBase reqBase = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request;
// do somthing with reqBase.Cookies

Microsoft似乎想让我们从HttpRequestMessage获取cookie时惹恼我们.

It seems like Microsoft wanted to annoy us while reaching cookies from HttpRequestMessage.

是否保证request.Properties["MS_HttpContext"]永远不会为空?

Is it guaranteed that request.Properties["MS_HttpContext"] will never be null?

或者认为ajax请求是在ApiController的操作中处理的.我可以通过两种不同的方式来获取客户端的IP.

Or think that an ajax request is handled in an action of ApiController. I can reach IP of the client with two different ways.

var ip = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request.UserHostAddress

var ip = HttpContext.Current.Request.UserHostAddress

这两者有什么区别?

或者通常,我可以以不同的方式访问相同的请求/响应数据,例如Cookie,标头,请求者信息等.什么时候使用?我们可以说类似如果是ajax请求,由于缺少某些内容,不能保证HttpRequest正常工作,所以对于ajax请求,我们应该改用HttpRequestMessage"?

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"?

推荐答案

页面 UserControl 类作为GET -only属性.同样,其大多数属性也仅是GET(请参见 1 ). ASP.NET页使用此类来获取有关传入的HTTP请求的信息,例如读取客户端IP,Cookie,查询字符串等.重要的是,它是旧" System.Web程序集的一部分,该程序自.NET 1.1以来就存在

HttpRequest is present on the Page and UserControl classes 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

HttpRequestMessage 是.NET 4.5中的新增功能.它是 System.Net 的一部分.客户端和服务都可以使用它通过HTTP创建,发送和接收请求和响应.它取代了 HttpWebRequest ,在.NET 4.5中该版本已过时

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

在HttpRequestBase和HttpRequestWrapper上,我能做的最好是引用

On HttpRequestBase and HttpRequestWrapper, best I can do is to just quote the docs

HttpRequestWrapper类派生自HttpRequestBase类 并用作HttpRequest类的包装.此类公开 HttpRequest类的功能,并公开 HttpRequestBase类型. HttpRequestBase类使您可以替换 您中HttpRequest类的原始实现 具有自定义实现的应用程序,例如您执行时 在ASP.NET管道之外进行单元测试.

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.

这篇关于HttpRequest与HttpRequestMessage与HttpRequestBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆