Introducing asp.net core (Part 2)
Published: 2021-02-19 04:02:30

◇Understanding the MVC pattern

MVC stands for: Model View Controller. Asp.net uses this architectural pattern in development to ensure separation of concern ie separation of view logic from application logic and business logic. 
The MVC pattern ensures that the model, views and controllers are separated into different classes with specific responsibilities. 

The view is everything that gets rendered to the user in the browser, which includes html,css and javascript. The views core responsibility is to deliver a good-looking user experience, so it should contain as little logic as possible. The view can always interact and call back on the controller for logic.

The controller is like the traffic cop of the application. It controls application workflow. It orchestrates the interaction between the model and the view.

The model is the heart of the application. 
It contains data access and business logic such as: business processes, validation rules and system integration logic.

◇ Handling requests with controllers

Controllers are merely classes and it is common practice to put all controller classes in one folder. So, setup our controller we:
1. Create a folder and name it "Controller "
2.write click on the folder and select add new item
3. Select MVC controller class and name the controller with the extension .cs.

On creation of the controller, asp.net automatically adds a public IActionresult method to the class. All controllers essentially are methods that maps each url request matching the name of the method to an Actionresult type. In the case of the method:
 Public IActionresult Index() 
{
   return view()
}

The Index method would return a view matching the name in the view folder.

◇ Passing parameters to controller action methods.

Controller IActionresult methods are normal c# methods, hence we can pass parameters to this methods to enhance there functions. Example, in the code:

Public IActionResult Post(int id)
{
  return new ContentResult(content=id)
}
 
This post action method takes a parameter "id", and the "Content Results" return type displays that id on the screen.

In a case whereby a string value is provided instead of the specified integer value for the "id" parameter, asp.net displays the default value which is 0.
It is standard practice to specify the default value of a parameter so as to know when a user has inputed a non-matching data type. To do this the code is written as:

Public IActionResult Post(int id=null)
{
  return new ContentResult(content=id)
}
 
When a string or boolean value is inputed, asp.net displays "null" instead of 0.


◇Understanding Routing.

The default rout pattern of an asp.net application is defined in the startup.cs class as follows: "{Controller=Home}/{Action=Index}/{id?}"

This means that the first part of the url must match a particular controller, in a case where it is not defined it takes on the default which is "Home". The second part maps to an action method in the controller, if it is not specified it maps to the default "Index". The ? attached the id means that it is an optional parameter.


◇Customising application url.

Sometimes we might need to use a custom url asides the generic one specified in the startup.cs class. To do this we make use of the route attribute either o  the controller or its action methods. For example, to map a blog/Post url to a different url  such as blog/2021/Adam (which is the year of a post/author) we change the code to:

Route[("blog/{year:int}/{author}/{int id}")]
Public IActionResult Post(int year, string author,int id)
{
  return new ContentResult(content=id)
}