MVC Contrib VerificationException

时间:2010-02-19 18:23:03

标签: c# asp.net-mvc mvccontrib

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

我的控制器声明如下:

   public class ProductsSearchController : Controller
   {
        ...
   }

请帮帮我。 我还尝试从here下载最新的MvcContrib库。它没有帮助我。

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

this.RedirectToAction(x => x.Index(filter))

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

供应搜索?过滤= WebShop.FinderModel.Filters.ProductsFilter

有什么问题?

1 个答案:

答案 0 :(得分: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>

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

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

如果您正在使用代码分析,则还需要查看Assembly Binding Redirection and Code Analysis,以使其尊重绑定重定向。

相关问题