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 {
-
-
-
-
-
-
- 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();
-
- return info;
- }
-
-
-
-
-
-
- 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();
-
- bindingContext.Model = info;
- return true;
- }
- private string GetLoggedUser() {
-
- return "3242423423";
- }
- }
- }
We use a model binder as a parameter of the MVC and Web API Action Result.
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);
- }
- }
- }
The use of Model Binder in MVC is given.
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:
Global.asax.cs
- public class MvcApplication: System.Web.HttpApplication {
- protected void Application_Start() {
- ModelBinders.Binders.Add(typeof(Models.EnvironmentInfo), new Binders.EnvironmentInfoModelBinder());
- }
- }
The Model Binder configuration for Web API
App_Start/WebApiConfig.cs
- public static class WebApiConfig {
- public static void Register(HttpConfiguration config) {
- config.BindParameter(typeof(Models.EnvironmentInfo), new Binders.EnvironmentInfoModelBinder());
- }
- }
Let me know, if you have others scenarios for model binders.
Download full source
code.
I hope, it helps.