无法从 HttpContextBase 转换为 HttpContextBase

发布于 2024-09-08 18:28:37 字数 1361 浏览 4 评论 0原文

使用以下代码(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半枫 2024-09-15 18:28:38

我发现(感谢 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 in System.Web.dll, Version 4.0.0.0 and in System.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 contain HttpContextBase.

So, I'm still not sure what's going on, but at least it's compiling now.

多情出卖 2024-09-15 18:28:38

可能是 System.WebHttpContextBase 中使用了 System.Web.Abstractions 中的某些内容。

因此,HttpContextBase 类可能与 System.Web.Abstractions 相关。

May be System.Web uses something in System.Web.Abstractions within HttpContextBase.

So, probably HttpContextBase Class has something tied to System.Web.Abstractions.

避讳 2024-09-15 18:28:38

您似乎引用了不同版本的 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.

羁客 2024-09-15 18:28:38

我想知道是否可以通过程序集重定向来解决此问题,而不是在 .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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文