How to provide optional parameters

May 5 at 7:42 AM
Hi!
I have a container cofigured to use with the MVC5 project. And I have a problem when trying to invoke some action with an optional parameters. For example
public class TestController : Controller
{
    public ActionResult List(int? page = null, string query = null)
    {
        return View();
    }
}
This gets failed with the ArgumentException:
Additional information: The parameters dictionary contains an invalid entry for parameter 'page' for method 'System.Web.Mvc.ActionResult List(System.Nullable`1[System.Int32], System.String)' in 'DynamicModule.ns.Wrapped_TestController_b1c7e20605214fbebc9c075b9c8cacba'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable`1[System.Int32]'.
Is it possible to solve this behavior?
Editor
May 6 at 4:36 AM
Unfortunately, I'm not able to recreate the issue. Can you post additional information. For example:
  • Are you using ASP.NET MVC Bootstrapper?
  • How are you registering the classes?
And anything else that you think could be helpful.

I can see that it looks like you are using interception but TestController does not have appear to have any interceptable methods. Do you still see the issue if interception is not enabled?

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
May 6 at 6:17 AM
I'm sorry for miss info. And you are right. I've configured interception for the controllers with the VirtualMethodInterceptor.
    container.AddNewExtension<Interception>();
    container.Configure<Interception>().SetDefaultInterceptorFor<HomeController>(new VirtualMethodInterceptor());
And the HomeController looks like
public class HomeController : Controller
{
    private readonly IUnityContainer container;

    public HomeController(IUnityContainer container)
    {
        this.container = container;
    }

    public virtual ActionResult Index(int? page = null, string query = null)
    {
        return View();
    }
    ...
}
I have made the test project that reprodice the error. If remove virtual from the controller action all works fine.
TestWebApp
Editor
May 25 at 8:04 AM
From what I am seeing it looks like Unity interception does not play well with MVC default parameter values. The scenario that you posted works when just using Unity alone (i.e. not using ASP.NET MVC).

Unity looks to be setting the DefaultValue for the derived proxy class to System.Reflection.Missing. The issue arises because ASP.NET MVC is using reflection to look up parameter types and values (for caching) and it's complaining when it encounters System.Reflection.Missing instead of int?.

The workaround would be to use interface interception instead of virtual method interception if you hit default value issues with interception. A more permanent solution might be modify Unity's code generation to explicitly set the default values (I haven't looked to see if there are any issues with doing that).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to