7

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 ?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.