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 ?
In C#, you are allowed to create a reference variable of an interface type or in other words, you are allowed to create an interface reference variable. Such kind of variable can refer to any object that implements its interface.
Simply framework will fail to create those controllers object that have parameterized constructor. In that case we need to create controller objects by our self and inject controller parameters there.
ASP MVC requires that all controllers implement IController interface. A controller doesn't need to derive from Controller or ControllerBase class. You can find an example of a custom controller implementing the IController interface in Adam Freeman's book.
A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With