MVC Contrib VerificationException
Asked Answered
C

1

10

I have read this post and I wanted to use ControllerExtensions.RedirectToAction method. But I have System.Security.VerificationException Which says: type argument '[MyController type]' violates the constraint of type parameter 'T'.

My controller is declared as follows:

   public class ProductsSearchController : Controller
   {
        ...
   }

Help me, please. Also I tried to download the latest MvcContrib library from here. It didn't help me.

I noticed an interesting fact. I have this exception only when calling from unit tests. But there is no exception when using from web site. However it seems not working correctly. When I pass an object to the action in expression like this:

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

it just call .ToString of this object! And I get url like this:

ProductsSearch?filter=WebShop.FinderModel.Filters.ProductsFilter

What is wrong?

Craps answered 19/2, 2010 at 18:23 Comment(2)
Could you show the line where you are redirecting?Gustative
return this.RedirectToAction(x => x.Index(filter));Craps
G
23

I've been having this problem.

I was using MvcContrib version 2.0.95.0 alongside System.Web.Mvc version 4.0.30319.

The problem was that MvcContrib references an earlier version of System.Web.Mvc.

If you're using an older version of MvcContrib with Mvc 2 it should be sufficient to download and reference the latest version of MvcContrib. If you're using .NET 4 and Mvc 3 you'll need to update the App.Config file for your unit test project (you may have to add one) with the following:-

<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>

Bear in mind that you may need to change the version numbers if you're using a different version of MVC. (e.g. at the time of this edit you'd need to use oldVersion="1.0.0.0-5.1.0.0" and newVersion="5.2.0.0").

You may also need to add this to your web project. If you're only getting the exception in your test project, chances are this section already exists and is correct in your web.config; you can copy and paste it from there.

If you're using Code Analysis, you'll also need to see Assembly Binding Redirection and Code Analysis in order for it to respect the binding redirection.

Geek answered 29/10, 2010 at 8:35 Comment(2)
Was just having this problem and found this post. Copied my runtime node from the Mvc project to the unit test project config file and it all worked. Thanks from me too :)Keratosis
This works for MVC4 (in beta as of today) at the moment as well. Just change the olderversion to be "1.0.0.0-3.0.0.0" and newVersion to "4.0.0.0".Calore

© 2022 - 2024 — McMap. All rights reserved.