Book Image

ASP.NET Core 2 Fundamentals

By : Onur Gumus, Mugilan T. S. Ragupathi
Book Image

ASP.NET Core 2 Fundamentals

By: Onur Gumus, Mugilan T. S. Ragupathi

Overview of this book

The book sets the stage with an introduction to web applications and helps you build an understanding of the tried-and-true MVC architecture. You learn all about views, from what is the Razor view engine to tagging helpers. You gain insight into what models are, how to bind them, and how to migrate database using the correct model. As you get comfortable with the world of ASP.NET, you learn about validation and routing. You also learn the advanced concepts, such as designing Rest Buy (a RESTful shopping cart application), creating entities for it, and creating EF context and migrations. By the time you are done reading the book, you will be able to optimally use ASP.NET to develop, unit test, and deploy applications like a pro.
Table of Contents (14 chapters)

Route Constraints


Route Constraints enable you to constrain the type of values that you pass to the controller action. It is all about action selection for routing. So, we can say one action should trigger for integer input whereas another should trigger for non-integers. For example, if you want to restrict the value to be passed to the int type, you can do so. The following is one such instance:

[HttpGet("details2/{id:int}")]
  public IActionResult Details2(int id = 123)
  {
    return View();
  }

ASP.NET Core even supports default parameter values so that you can pass the default parameters:

[HttpGet("details2/{id:int}")]
  public IActionResult Details2(int id = 123)
  {
    return View();
  }

There are many constraints available, such as int and bool.

Activity: Creating an Attribute that Implements IActionConstraintFactory

Scenario

By using IActionConstraint and IActionConstraintFactory interfaces we can add custom constraints. Create an attribute that implements IActionConstraintFactory and...