How To Control Model Binding in ASP.Net Core ?

Introduction

ASP.Net Core MVC enables developers to build data-driven web applications. The data received from an HTTP request is mapped to input parameters, both simple (strings, integers, floats, etc.) and complex. This mapping process is referred to as Model Binding, an MVC feature that has helped ASP.Net Core developers from rewriting similar mapping code for every application.

Mapping the data received from the HTTP request (regardless of its size or complexity) to its counterpart is usually repetitive and can get tedious. Model binding saves developers from writing their own error-prone text to type converter code.

Understanding the Concept of Model Binding

Upon receiving an HTTP request, ASP.Net Core MVC routes the request to an action method of controller based on the route data provided. Next, the request data is bound to inputs, i.e., the parameters of action method. For instance, consider this simple URL:

https://www.example.com/blog/posts/12

Taking a quick look at the URL offers us clear insights that it is trying to retrieve a blog post on example.com with id 12. However, things are not this clear to ASP.Net. It has to parse the URL and map it with the help of model binding. The default route template that ASP.net Core MVC uses to map URLs looks something like this:

{controller}/{action}/{id?}

Thus, the URL routes to Blog controller and its Posts action, including an optional parameter with name id and value 12.

Take note that the string parameters in the URL route are case insensitive.

List of Data Sources

ASP.net Core MVC usually binds HTTP request data to action method parameters by name. In the Blog posts example, the only action parameter is id, and the data is mapped from the routing system with same name. However, apart from route values, there are other data sources used by MVC to map HTTP requests to action methods. Here is the list of sources in order:

  1. Form Values: These are the form values present in HTTP POST requests.
  2. Route Values: Route values provided by the Routing system.
  3. Query Strings: These are the values offered in query string of the URL.

All of these data sources are kept in the form of name-value pairs.

Model Binding with Complex Data Types

The id parameter used in our Blog post example is of primitive type (int, string, etc.). When it comes to mapping complex types, MVC uses both reflection and recursion for binding.

Let’s consider the following example:

public class Customer

{

public string FirstName { get; set; }

}

We have a Customer class, and a URL as specified below:

https://www.example.com/customer/create?firstName=Alex

Reflection is used by MVC to find that Customer class has a property called FirstName. Then, the value β€œAlex” found in query string is bound to the firstName parameter’s value.

Customizing Model Binding

ASP.Net Core MVC offers several attributes to override the default model binding. For instance, you can specify whether a property requires binding or not with [BindRequired] & [BindNever] attributes respectively. Attributes form an excellent tool in MVC and offer a customized approach to perform model binding.

Implementing Model Binding requires skill and knowledge of experts who know what it takes to get the data mapped correctly. You can always hire ASP.Net freelancers in USA or reach out to a professional ASP.Net development company.

 

Leave a Comment