- It converts client request data (form values, route data, query string parameters, HTTP headers) into objects that the controller can handle.
- As a result, your controller logic doesn't have to do the work of figuring out the incoming request data; it simply has the data as parameters to its action methods.
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) { ... }
- Validation is supported by decorating your model object with data annotation validation attributes.
- The validation attributes are checked on the client side before values are posted to the server, as well as on the server before the controller action is called.
using System.ComponentModel.DataAnnotations;
public class LoginViewModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
// work with the model
}
// At this point, something failed, redisplay form
return View(model);
}
- The framework handles validating request data both on the client and on the server.
- Validation logic specified on model types is added to the rendered views as unobtrusive annotations and is enforced in the browser with jQuery Validation.
- They help developers encapsulate cross-cutting concerns, like exception handling or authorization.
- Filters enable running custom pre- and post-processing logic for action methods, and can be configured to run at certain points within the execution pipeline for a given request.
- Filters can be applied to controllers or actions as attributes (or can be run globally).
- Several filters (such as
Authorize
) are included in the framework.
- They provide a way to partition a large ASP.NET Core MVC Web app into smaller functional groupings.
- An area is an MVC structure inside an application.
- In an MVC project, logical components like Model, Controller, and View are kept in different folders, and MVC uses naming conventions to create the relationship between these components.