I've created a custom MVC Model Binder which gets called for every HttpPost
that comes into the server. But does not get called for HttpGet
requests.
- Should my custom model binder get called during a
GET
? If so, what did I miss? - If not, How can I write custom code handling the
QueryString
from aGET
Request?
Here's my implementation...
public class CustomModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// This only gets called for POST requests. But I need this code for GET requests.
}
}
Global.asax
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
//...
}
I've looked into these solutions, but they don't quite work for what I'm looking for:
- Persisting complex types via
TempData
- Using the default binder to build up complex types (
?Name=John&Surname=Doe
)
Remark on answer
Thanks to @Felipe for the help. Just in case someone struggles with the same, I learnt:
- The custom model binder CAN be used for
GET
requests - You CAN use
DefaultModelBinder
class - My snag was that the action method MUST have a parameter (otherwise the model binder is skipped for
GET
Requests, which makes sense when you think about it)
Request
. – VittoriaQueryString
in theOnActionExecuting
. I wouldn't be able to translate the QueryString into an object and pass it as a parameter to the action method right? Which leaves me with the same predicament. – Florist