views:

124

answers:

2

Hello all. 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?

A: 

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 (you may have to add one) in your unit test project 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>

You may need to change the version numbers. You may also need to add this to your web project.

If you're using Code Analysis, you'll also need to see http://stackoverflow.com/questions/4042855/assembly-binding-redirection-and-code-analysys in order for it to respect the binding redirection.

Iain Galloway