无法从 HttpContextBase 转换为 HttpContextBase
使用以下代码(使用 Moq 4.0.10501.6):
HomeController controller = new HomeController();
ActionResult result = _controller.Index();
Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();
Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
httpContext.Setup(x => x.Response).Returns(response.Object);
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.HttpContext).Returns(httpContext.Object);
result.ExecuteResult(controllerContext.Object);
...我收到以下编译器错误:
error CS1502: The best overloaded method match for
'Moq.Language.IReturns<System.Web.Mvc.ControllerContext,
System.Web.HttpContextBase>.Returns(System.Web.HttpContextBase)'
has some invalid arguments
error CS1503: Argument 1: cannot convert from 'System.Web.HttpContextBase
[c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\
v4.0\System.Web.dll]' to 'System.Web.HttpContextBase'
我做错了什么?为什么我无法从 HttpContextBase
转换为 HttpContextBase
?
我从 ASP.NET MVC 项目开始,添加了一个 NUnit 测试项目,然后 ReSharper 整理了缺失的 System.Web 引用。根据VS中的属性窗口,我引用的System.Web.dll是C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System中的System.Web.dll .Web.dll。
With the following code (using Moq 4.0.10501.6):
HomeController controller = new HomeController();
ActionResult result = _controller.Index();
Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();
Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
httpContext.Setup(x => x.Response).Returns(response.Object);
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.HttpContext).Returns(httpContext.Object);
result.ExecuteResult(controllerContext.Object);
...I get the following compiler errors:
error CS1502: The best overloaded method match for
'Moq.Language.IReturns<System.Web.Mvc.ControllerContext,
System.Web.HttpContextBase>.Returns(System.Web.HttpContextBase)'
has some invalid arguments
error CS1503: Argument 1: cannot convert from 'System.Web.HttpContextBase
[c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\
v4.0\System.Web.dll]' to 'System.Web.HttpContextBase'
What have I done wrong? Why can't I convert from HttpContextBase
to HttpContextBase
?
I started with an ASP.NET MVC project, added an NUnit test project and ReSharper sorted out the missing System.Web reference. According to the properties window in VS, the System.Web.dll that I'm referencing is the one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.dll
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现(感谢 Reflector)
HttpContextBase
实际上位于System.Web.dll,版本 4.0.0.0
和System.Web 中.Abstractions,版本 3.5.0.0
。因此,我通过向项目添加对
System.Web.Abstractions,版本 4.0.0.0
的引用来修复此问题。然而奇怪的是,
System.Web.Abstractions,版本 4.0.0.0
不包含HttpContextBase
。所以,我仍然不确定发生了什么,但至少现在正在编译。
I discovered (thanks Reflector) that
HttpContextBase
is actually inSystem.Web.dll, Version 4.0.0.0
and inSystem.Web.Abstractions, Version 3.5.0.0
.So, I fixed it by adding a reference to
System.Web.Abstractions, Version 4.0.0.0
to the project.Oddly, however,
System.Web.Abstractions, Version 4.0.0.0
doesn't containHttpContextBase
.So, I'm still not sure what's going on, but at least it's compiling now.
可能是
System.Web
在HttpContextBase
中使用了System.Web.Abstractions
中的某些内容。因此,
HttpContextBase
类可能与System.Web.Abstractions
相关。May be
System.Web
uses something inSystem.Web.Abstractions
withinHttpContextBase
.So, probably
HttpContextBase
Class has something tied toSystem.Web.Abstractions
.您似乎引用了不同版本的 System.Web.Mvc 程序集。尝试删除项目中对此程序集的所有引用,然后从 GAC 添加它。
It seems that you have referenced different versions of the
System.Web.Mvc
assembly. Try removing all references to this assembly in your projects and adding it from the GAC.我想知道是否可以通过程序集重定向来解决此问题,而不是在 .NET 4.0 项目中引用 .NET 3.5 程序集?
I wonder if this could be fixed with an assembly redirect instead of referencing a .NET 3.5 assembly in a .NET 4.0 project?