代码之家  ›  专栏  ›  技术社区  ›  Serhiy

MVC控制验证异常

  •  10
  • Serhiy  · 技术社区  · 14 年前

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

    我的控制器声明如下:

       public class ProductsSearchController : Controller
       {
            ...
       }
    

    请帮帮我。 我还尝试从下载最新的mvcontrib库 here . 这对我没有帮助。

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

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

    它只是调用。这个对象的字符串!我得到这样的网址:

    产品搜索?filter=webshop.finderModel.filters.productsfilter

    怎么了?

    1 回复  |  直到 9 年前
        1
  •  23
  •   Community kfsone    7 年前

    我一直有这个问题。

    我使用的是MVCContrib 2.0.95.0版和System.Web.MVC 4.0.30319。

    问题是mvcontrib引用了早期版本的system.web.mvc。

    如果您使用的是旧版本的mvcontrib和mvc 2,那么下载并引用最新版本的mvcontrib就足够了。如果您使用的是.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项目中。如果您只在测试项目中获得异常,那么很可能这个部分已经存在,并且在web.config中是正确的;您可以从那里复制并粘贴它。

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

    推荐文章