MVC Contrib VerificationException

Ser*_*hiy 10 c# asp.net-mvc mvccontrib

我已阅读此帖,我想使用ControllerExtensions.RedirectToAction方法.但我有System.Security.VerificationException其中说:类型参数'[MyController type]'违反了类型参数'T'的约束.

我的控制器声明如下:

   public class ProductsSearchController : Controller
   {
        ...
   }
Run Code Online (Sandbox Code Playgroud)

请帮帮我.我还尝试从这里下载最新的MvcContrib库.它没有帮助我.

我注意到一个有趣的事实.只有在从单元测试中调用时才会出现此异常.但是从网站上使用时也不例外.但它似乎无法正常工作.当我将对象传递给表达式中的动作时,如下所示:

this.RedirectToAction(x => x.Index(filter))
Run Code Online (Sandbox Code Playgroud)

它只是调用.ToString这个对象!我得到这样的网址:

产品搜索?过滤= WebShop.FinderModel.Filters.ProductsFilter

怎么了?

Iai*_*way 23

我一直有这个问题.

我使用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>
Run Code Online (Sandbox Code Playgroud)

请记住,如果您使用的是不同版本的MVC,则可能需要更改版本号.(例如,在编辑本文时,您需要使用oldVersion="1.0.0.0-5.1.0.0"newVersion="5.2.0.0").

您可能还需要将此添加到您的Web项目中.如果您只是在测试项目中获得异常,则此部分可能已存在并且在您的web.config中是正确的.你可以从那里复制并粘贴它.

如果您正在使用代码分析,则还需要查看程序集绑定重定向和代码分析,以使其遵守绑定重定向.

  • 这也适用于MVC4(目前处于测试阶段).只需将旧版本更改为"1.0.0.0-3.0.0.0",将newVersion更改为"4.0.0.0". (3认同)