Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Contrib VerificationException

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?

like image 424
Serhiy Avatar asked Feb 19 '10 18:02

Serhiy


1 Answers

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.

like image 125
Iain Galloway Avatar answered Sep 20 '22 22:09

Iain Galloway