Chapter 10. Model binders and value providers

 

This chapter covers

  • Examining model binding
  • Creating a custom model binder
  • Extending value providers

The messaging protocol of the web, HTTP, is decidedly string-centric. Query-string and form values in Web Forms and even classic ASP applications were represented as loosely typed key-value string dictionaries. But with the simplicity of controllers and actions came the ability to treat requests as method calls, and to post variables as parameters to a method. To keep the dictionary abstractions at bay, you need a mechanism to translate string-based input into strongly typed objects. By default, ASP.NET MVC will translate request variables into a format you can easily work with. However, you’ll often see additional shaping of the model being used, whether it’s loading information from a database or pulling data from additional stores such as cookies, session variables, and configuration values.

In the last chapter, we looked at using routes to build custom URL schemes. In this chapter, we’ll examine the abstractions ASP.NET MVC uses to translate request variables into action parameters and the extension points that allow us to add our own translation logic. We’ll use these extension points to remove additional model building logic out of our controllers.

10.1. Creating a custom model binder

10.2. Using custom value providers

10.3. Summary