The goal is to show how to create a custom binder to use in both MVC and WEB API applications.
The classic use of model binder is to capture information of a form but here, my Model Binder has the responsibility to hold information about the environment like logged user, URL and other things.
What we need?
- MVC Application.
- Web API Application.
- Model Binder itself.
I have created one Application with MVC and Web API together.
Let's get started.
The model binder class with both MVC and Web API implementations.
Binders\EnvironmentInfoModelBinder.cs
- namespace FSL.ModelBinderInMvcAndWebApi.Binders {
- public class EnvironmentInfoModelBinder: System.Web.Mvc.IModelBinder, System.Web.Http.ModelBinding.IModelBinder {
- /// <summary>
- /// MVC version
- /// </summary>
- /// <param name="controllerContext"></param>
- /// <param name="bindingContext"></param>
- /// <returns></returns>
- public object BindModel(System.Web.Mvc.ControllerContext controllerContext,
- System.Web.Mvc.ModelBindingContext bindingContext) {
- var info = new Models.EnvironmentInfo();
- info.RequestedUrl = controllerContext.RequestContext.HttpContext.Request.Url.ToString();
- info.UserId = GetLoggedUser();
- // you also can get form/request properties
- return info;
- }
- /// <summary>
- /// WEB API version
- /// </summary>
- /// <param name="actionContext"></param>
- /// <param name="bindingContext"></param>
- /// <returns></returns>
- public bool BindModel(System.Web.Http.Controllers.HttpActionContext actionContext,
- System.Web.Http.ModelBinding.ModelBindingContext bindingContext) {
- var info = new Models.EnvironmentInfo();
- info.RequestedUrl = actionContext.Request.RequestUri.ToString();
- info.UserId = GetLoggedUser();
- // you also can get form/request properties
- bindingContext.Model = info;
- return true;
- }
- private string GetLoggedUser() {
- //just a sample
- return "3242423423";
- }
- }
- }
The parameter environmentInfo will contain all the information we want about the URL and logged user and other things.
The use of Model Binder for Web API is given.
Services/Controllers/PersonApiController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using FSL.ModelBinderInMvcAndWebApi.Models;
- namespace FSL.ModelBinderInMvcAndWebApi.Services.Controllers {
- [RoutePrefix("api/person")]
- public class PersonApiController: ApiController {
- [Route("")]
- public IHttpActionResult Get(Models.EnvironmentInfo environmentInfo) {
- var result = new {
- EnvironmentInfo = environmentInfo
- };
- return Ok(result);
- }
- }
- }
Controllers/HomeController.cs
- using FSL.ModelBinderInMvcAndWebApi.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace FSL.ModelBinderInMvcAndWebApi.Controllers {
- public class HomeController: Controller {
- public ActionResult Index(Models.EnvironmentInfo environmentInfo) {
- return View();
- }
- }
- }
- The Model Binder configuration
- for MVC:
- public class MvcApplication: System.Web.HttpApplication {
- protected void Application_Start() {
- ModelBinders.Binders.Add(typeof(Models.EnvironmentInfo), new Binders.EnvironmentInfoModelBinder());
- }
- }
App_Start/WebApiConfig.cs
- public static class WebApiConfig {
- public static void Register(HttpConfiguration config) {
- config.BindParameter(typeof(Models.EnvironmentInfo), new Binders.EnvironmentInfoModelBinder());
- }
- }
Download full source code.
I hope, it helps.

Manav PandyaPosted Dec 10, 2016, 6:35 AM
Nicely explained article sir @fabio