我了解 HttpPostedFileBase
和 HttpPostedFileWrapper
之间的关系,就两者的需求而言(即在单元测试/模拟中)。但是,为什么当我在 HttpPostedFileBase
的返回值上设置断点时,它是否显示为 HttpPostedFileWrapper
?
此外,HttpPostedFileBase
没有实现 ContentType 属性。那么,当我的代码仅 引用HttpPostedFileBase
而不是HttpPostedFileWrapper
时,为什么它会返回一个值?这是什么诡计?
编辑#1:
感谢@lawliet29 的精彩回复。我已经按照建议写出了结构。
public sealed class MyHttpPostedFile
{
public string ContentType { get { return "123"; } }
}
public abstract class MyHttpPostedFileBase
{
}
public class MyHttpPostedFileWrapper : MyHttpPostedFileBase
{
private MyHttpPostedFile _myHttpPostedFile;
public MyHttpPostedFileWrapper(MyHttpPostedFile myHttpPostedFile) {
_myHttpPostedFile = myHttpPostedFile;
}
public string ContentType { get { return _myHttpPostedFile.ContentType; } }
}
为了让它工作,我需要像这样传递参数:
GetFiles(new MyHttpPostedFileWrapper(new MyHttpPostedFile());
这似乎是我质疑的诡计存在的地方。 .NET 如何知道传递给它的字节是 MyHttpPostedFile
类型的类,并且它应该获取该对象并将其作为参数传递到我的构造函数中?
编辑#2:
我没有意识到 ASP.NET MVC 绑定(bind)器除了通过传递这些更高级别的对象来传递字节之外还能做更多的事情。这就是我想知道的诡计!感谢您的精彩回复。
最佳答案
其实很简单。 HttpPostedFileBase 是一个抽象类,仅用于被派生的目的。它被用来使密封类 HttpPostedFile 中的某些东西可以模拟。
然而,在现实生活中,HttpPostedFile 是处理已发布文件的工具,为了保持一致,创建了 HttpPostedFileWrapper。此类通过包装 HttpPostedFile 为 HttpPostedFileBase 提供实现。
所以HttpPostedFileBase是一个统一的抽象,HttpPostedFile是一个表示posted文件的类,HttpPostedFileWrapper是HttpPostedFileBase的实现,它包装了HttpPostedFile。 HttpPostedFileWrapper 的 ContentType 属性的实现从底层 HttpPostedFile 读取内容类型。
编辑:某种解释
ASP.NET MVC 接收到一个文件,并且在其下方的某处创建了一个 HttpPostedFile 实例,因为这是自 .NET Framework 1.0 以来的工作方式。 HttpPostedFile 的定义如下所示:
public sealed class HttpPostedFile
这基本上意味着它不能被继承,也不能为单元测试而模拟。
为解决此问题,ASP.NET MVC 开发人员创建了一个可模拟的抽象 - HttpPostedFileBase,其定义如下:
public abstract class HttpPostedFileBase
现在,您可以定义 MVC 操作,以便它们接受 HttpPostedFileBase 而不是不可模拟的 HttpPostedFile:
[HttpPost]
public ActionResult PostFile(HttpPostedFileBase file)
{
// some logic here...
}
问题是,在下面的某个地方,表示已发布文件的唯一方法是老式的死板 HttpPostedFile。因此,为了支持这种抽象,MVC 开发人员创建了一个名为 HttpPostedFileWrapper 的装饰器,大致如下所示:
public class HttpPostedFileWrapper : HttpPostedFileBase
{
private HttpPostedFile _httpPostedFile;
public HttpPostedFileWrapper(HttpPostedFile httpPostedFile) {
_httpPostedFile = httpPostedFile;
}
public string ContentType { get { return _httpPostedFile.ContentType; } }
// implementation of other HttpPostedFileBase members
}
因此,现在 HttpPostedFileWrapper 是您在使用已发布的文件执行真正的 HTTP POST 请求时实际得到的。由于多态性,您可以将派生类的实例 - HttpPostedFileWrapper - 传递给方法接受基类 - HttpPostedFileBase。
一直以来,您都可以创建自己的模拟实现,比如说,看起来就像正在发布的视频文件。你会这样做
public class MockPostedVideoFile : HttpPostedFileBase
{
public string ContentType { get { return "video/mp4"; } }
// rest of implementation here
}
另一个编辑: HttpPostedFile 的实际实例化全部由 System.Web 为您处理。 ASP.NET MVC 绑定(bind)器对于发布的表单数据非常智能。它会自动检测到某些 post 值实际上是文件的字节,因此为了正确表示它们,它可以使用 System.Web 框架中的旧内容来创建一个实例 HttpPostedFile。
这里的要点是 - 您无需担心。这里的幕后发生了很多事情,我们真的需要感谢 ASP.NET MVC 团队将所有这些低级别的事情抽象化。
您唯一需要担心的地方是单元测试。在您的测试中,您可以使用模拟实现来调用您的操作,如下所示:
myController.PostFile(new MockPostedVideoFile())
关于c# - HttpPostedFileBase 与 HttpPostedFileWrapper 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545216/