Task2_ Web Application and RESTful API DevelopmentWeb Application and RESTful API Development
Task2_ Web Application and RESTful API DevelopmentWeb Application and RESTful API Development
When using MVC we are forced to learn its main components such as controllers,
models and views. MVC offers a communication where the controller can obtain
information from the models and supply that data to the views, this is the most
common form of communication in MVC. Now the views cannot request information
from the models directly, they must create a request that is analyzed by the specific
controller of that view, this controller is responsible for consulting the corresponding
model and based on the logic that is detailed in said models, the information is
provided to the view upon returning as a response.
We can see an example of this in the part of the forms and in the data that the user
wants to process. Once we are located in our form in the view, it will ask us for
certain data that is necessary for the logic of the application, in a hypothetical case, if
we are registering a new user to our application we must register all their personal
information since the system forces us to enter that information. If there are fields
such as email or age, through a component of the models that we call User we can
assign certain validations to these fields, for example that the age has to be over 18
years old and under 75 years old, or that the user actually enters a valid email that
has the corresponding signs. We can carry out all these types of validations both on
the model side and on the controller side.
An example of how we can limit an age range is as follows: we type in our model the
Age property [Range(18, 60)]. This means that the age will only allow us to enter
values between 18 and 60 but not those that are less than 18 or more than 60 years
old.In the same way, if we use a login view, through controllers we can verify that the
data is correct for the user who is trying to enter the application and also save the
session.
When we want to create our Web API we must always take into account the
language on which we want our service to be based. Once we have selected our
programming language, all that remains is to start typing the code. A facility that
ASP.NET Core offers us is that it is more focused on the creation of this type of
services since it provides us with many tools that help us both visually and at the
structuring level, since it becomes easy to read and understand. Let's review the
step by step in creating the Web APIs in ASPE.NET Core:
Code Examples
Now we are going to illustrate some code examples that will be helpful to better
understand the structure of this type of projects:
Web API Project
MVC Setup
Data Validation
Data validation is a fundamental part of the models since these will define how we
are going to work with the information that comes to us as well as what type of
information we accept. There are many types of validations that we can do and use
in our applications, this way we will be able to manage the information in a format
that is more acceptable to us, either due to database restrictions or because it is the
easiest way to do it.