tags:

views:

117

answers:

2

Can a ASP .NET Controller action method take an interface as one of the parameters ?

I would like to have something like:

class MyController 
{
    [HttpPost]
    public ActionResult Action(IMyModel model) {...}
}

Is it possible ? Obviously I would have to tell the framework which concrete implementation of IMyModel that should be instantiated, but how ?

+3  A: 

More or less everything is extensible in ASP.NET MVC so yes; that should be possible. Have a look at Model Binders, I think that is what you're looking for.

CodingInsomnia
+1  A: 

Out of the box, no. As andiju said, however, everything is pluggable in MVC. Given the pattern you're after, I'd recommend looking at using an IoC container (Windsor, Unity, StructureMap) and then setting up a IoC-based modelbinder and an IoC-based controllerbuilder. That'd be the most flexible approach and would also be the least headache (I think) in terms of actually resolving those concrete types over the life of your app.

Paul