Model Binding in ASP.NET Core Interview Question & answers

Introduction

Today, we’ll discuss the Common interview questions asked in Model Binding in ASP.NET Core. First, we will go through what Model Binding is in ASP.NET Core. Model Binding in ASP.NET Core is a fundamental and powerful feature that simplifies the process of mapping HTTP request data to C# model objects. I hope you understand the Basic Concepts of Model Binding in ASP.NET Core MVC.

📌 .NET Core Interview Prep – Model Binding & Validation (MVC + Web API)

Below are some Interview questions specific to Model Binding in ASP.NET Core. I hope you will find it really helpful. I have created a structured Q&A list that focuses solely on Model Binding & Validation (for both ASP.NET Core MVC & Web API). If you’re not aware of the Model Binding in ASP.NET Core, you can refer to this article from the csharpcorner.

1. What is Model Binding in ASP.NET Core ?

Model Binding in ASP.NET Core is the process of mapping data from an HTTP request (route values, query string, headers, form values, body, etc.) to action method parameters and model properties automatically.

Example: If a request query string has ?id=10&name=Logiclense, ASP.NET Core binds it to an action method like:

public IActionResult GetUser(int id, string name)

2. What are the sources of data for Model Binding in MVC/Web API?

ASP.NET Core model binding looks for values in a specific order:

  1. Form values (POST body in form submissions)
  2. Route data (from URL patterns like /users/{id})
  3. Query string parameters (e.g., ?id=1)
  4. Headers (e.g., Authorization, custom headers)
  5. Body (for complex objects in Web API, usually JSON/XML)
  6. Files (uploaded files via IFormFile)

3. What happens if model binding fails in ASP.NET Core?

  • If binding fails, the parameter is set to its default value (e.g., null for reference types, 0 for int).
  • Validation errors are added to the ModelState dictionary, which can be checked in the controller.

4. How do you explicitly specify the source of model binding in ASP.NET Core?

By using attributes:

  • [FromQuery] → Binds from query string
  • [FromRoute] → Binds from route data
  • [FromBody] → Binds from request body (usually JSON/XML)
  • [FromForm] → Binds from form data
  • [FromHeader] → Binds from request headers
  • [FromServices] → Binds from DI container

Example:

public IActionResult Create([FromBody] User user, [FromHeader(Name = "X-Correlation-Id")] string correlationId)

5. Difference between [FromBody] and [FromForm]?

  • [FromBody] → Used for JSON/XML request body in Web API. Only one parameter can be bound from the body in an action.
  • [FromForm] → Used for form data (like HTML forms or multipart data). Supports multiple parameters.

6. What is Model Validation in ASP.NET Core?

Validation ensures incoming data is correct before processing. ASP.NET Core uses Data Annotations and Fluent Validation to validate model properties.

  • Example:
public class User {
     [Required]
     public string Name { get; set; }
 
    [Range(18, 60)]
    public int Age { get; set; }
}

7. How does Model Validation work with Model Binding?

  • After model binding, ASP.NET Core automatically validates model properties based on validation attributes.
  • Validation results are stored in ModelState.
  • Developers can check using:
if (!ModelState.IsValid) return BadRequest(ModelState);

8. Commonly used Validation Attributes in ASP.NET Core?

  • [Required] – Property must not be null/empty.
  • [StringLength(50)] – Restricts string length.
  • [Range(1,100)] – Restricts numeric values.
  • [EmailAddress] – Validates email format.
  • [Compare(“Password”)] – Matches two fields.
  • [RegularExpression(“pattern”)] – Validates using regex.
  • [BindNever] – Prevents binding.
  • [BindRequired] – Marks property as required during binding.

9. How to implement custom validation in ASP.NET Core?

We can implement the Custom Validation in ASP.NET Core in two ways:

  • Custom Attribute → Inherit from ValidationAttribute
public class EvenNumberAttribute : ValidationAttribute {
   public override bool IsValid(object value) {
     return value is int number && number % 2 == 0;   
     }
}
  • IValidatableObject Interface → Implement in Model
public class User : IValidatableObject {
    public string Name { get; set; }
    public int Age { get; set; }
    
   public IEnumerable<validationresult> Validate(ValidationContext validationContext) {
        if (Age < 18) {

           yield return new ValidationResult("Age must be at least 18", new[] { nameof(Age) });
    }
  }
}

10. How is validation handled differently in MVC vs Web API?

  • MVC → Invalid models are often shown back in the view with validation messages using tag helpers like asp-validation-for.
  • Web API → Invalid models return a 400 Bad Request response (if [ApiController] attribute is applied).

11. What is the role of [ApiController] in model validation?

  • Automatically checks ModelState.IsValid.
  • If invalid, returns 400 Bad Request with error details without writing explicit code.
  • Without [ApiController], you must manually check ModelState.

12. How do you enable client-side validation in ASP.NET Core MVC?

  • ASP.NET Core uses jQuery Validation and Unobtrusive Validation by default.
  • Add script references:
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
  • Use Tag Helpers like:
<input asp-for="Name" />
<span asp-validation-for="Name">
</span>

13. Can multiple parameters be bound from the body?

No. ASP.NET Core allows only one parameter from the request body ([FromBody]) because the body can only be read once. If needed, wrap multiple values inside a single DTO.

14. What is Bind Attribute in ASP.NET Core?

The [Bind] attribute specifies which properties should be included/excluded in model binding to prevent over-posting attacks.

public IActionResult Create([Bind("Name,Email")] User user)

15. Difference between ModelState.IsValid and TryValidateModel()?

  • ModelState.IsValid → Checks if the model binding + validation succeeded.
  • TryValidateModel(model) → Manually triggers validation on an already bound object. Useful when validating nested models or updating partial objects.

16. How do you handle validation globally in Web API?

  • Use [ApiController] attribute (automatic).
  • Alternatively, create a custom filter that implements IActionFilter or IAsyncActionFilter to check the ModelState.

17. What is the difference between IFormFile and [FromBody] file upload?

  • IFormFile → Used for multipart/form-data uploads (common in forms).
  • [FromBody] byte[] → Used when the file is uploaded as raw bytes in the request body.

18. Can model binding handle complex objects?

Yes. ASP.NET Core recursively binds data to complex types. Example:

public class Address { 
public string City { get; set; } 
}
public class User { 
public string Name { get; set; } 
public Address Address { get; set; } 
}

If the request JSON is:

{
  "name": "Shreya",
  "address": { "city": "Delhi" }
}

ASP.NET Core binds it correctly.

Let’s Connect 🔗

I hope this article about routing in ASP.NET Core helps you grow in your .NET developer journey in 2025 and beyond. I regularly share coding resources, learning roadmaps, project ideas, and career tips across multiple platforms. If you found this helpful, consider following me and joining the Logic Lense community!

📸Instagram – @logiclense

🎥 YouTube – Logic Lense

👨‍💻 LinkedIn – Connect with me

🌐 Website – www.logiclense.com

Let’s code, grow, and innovate — together.  Happy Learning!!!!

Subscribe to ASP.NET Core Newsletter.

Want to advance your career in .NET and Architecture? Join 1,000+ readers of my newsletter. Each week you will get 1 practical tip with best practices and real-world examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
0%