我读过这个post我想使用 ControllerExtensions.RedirectToAction 方法。但是我有 System.Security.VerificationException 说:类型参数“[MyController type]”违反了类型参数“T”的约束。
我的 Controller 声明如下:
public class ProductsSearchController : Controller
{
...
}
请帮帮我。 我还尝试从 here 下载最新的 MvcContrib 库.它没有帮助我。
我注意到一个有趣的事实。我只有在从单元测试调用时才有这个异常。但是从网站使用时也不异常(exception)。但是它似乎无法正常工作。当我将一个对象传递给这样的表达式中的操作时:
this.RedirectToAction(x => x.Index(filter))
它只是调用这个对象的.ToString!我得到这样的网址:
ProductsSearch?filter=WebShop.FinderModel.Filters.ProductsFilter
怎么了?
最佳答案
我一直有这个问题。
我使用的是 MvcContrib 版本 2.0.95.0 和 System.Web.Mvc 版本 4.0.30319。
问题是 MvcContrib 引用了早期版本的 System.Web.Mvc。
如果您将旧版本的 MvcContrib 与 Mvc 2 一起使用,那么下载和引用最新版本的 MvcContrib 应该就足够了。如果您使用的是 .NET 4 和 Mvc 3,则需要使用以下内容更新单元测试项目的 App.Config 文件(您可能需要添加一个):-
<configuration>
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
....
</configuration>
请记住,如果您使用不同版本的 MVC,您可能需要更改版本号。 (例如,在进行此编辑时,您需要使用 oldVersion="1.0.0.0-5.1.0.0"
和 newVersion="5.2.0.0"
)。
您可能还需要将其添加到您的网络项目中。如果你只是在你的测试项目中得到异常,那么这个部分很可能已经存在并且在你的 web.config 中是正确的;您可以从那里复制并粘贴它。
如果您使用代码分析,您还需要查看 Assembly Binding Redirection and Code Analysis为了让它遵守绑定(bind)重定向。
关于c# - MVC Contrib VerificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2298625/