ASP.Net Core MVC for beginners

This article includes the ASP.NET Core MVC web development introductions for beginners. If you are new to MVC, then read my earlier blogs on ASP.Net MVC.
This article explores the basics of building an ASP.NET Core MVC web app.
This article includes the following topics-

  • Introduction to ASP.Net Core MVC
  • Routing in ASP.Net Core MVC
  • Working with Controller
  • Handling View
  • Working with Model
  • Running a simple ASP.Net Core MVC web application

Introduction to ASP.Net Core MVC

ASP.NET Core MVC is a rich framework for building a web application and Restful APIs using the Model-View-Controller design pattern.

What is the MVC pattern?

The Model-View-Controller or MVC pattern separates an application into 3 components i.e. Models, Views, and Controllers.
With this pattern, HTTP requests are routed to a particular Controller which is associated with the Model to perform actions to retrieve results.

You may love to read –

What is ASP.NET Core MVC?

The ASP.NET Core MVC framework is a lightweight, open-source framework optimized for use with ASP.NET Core.
ASP.NET Core MVC provides a patterns-based way to build dynamic websites that enables a clean separation of concerns.

Routing in ASP.Net Core MVC

ASP.NET Core MVC is built on top of ASP.NET Core’s routing.
This enables you to define your web application’s URL naming patterns that is helpful in search engine optimization (SEO).
When an incoming request is received, the routing engine parses the URL and matches it to one of the defined Action and Controller.
There are 2 ways to define a route in ASP.Net Core MVC

Convention Based Routing

Convention-based routing enables you to globally define the URL formats that your application accepts and how each of those formats maps to a specific action method on the given controller.

Attribute Routing

Attribute routing enables you to specify routing information by decorating your controllers and actions with attributes that define your application’s routes.
This means that your route definitions are placed into each controller file.

A controller in ASP.Net Core MVC

Controllers Classes handle incoming requests.
They retrieve model data and call view templates to present the result to the user.
In the MVC design pattern, the Controller handles and responds to user inputs.

How to add a controller?

Right-click on the Controllers folder in Solution Explorer and select Add->Controller
Select MVC Controller from the dialogue box.
public class DefaultController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

View in ASP.Net Core MVC

In ASP.Net MVC and ASP.Net Core MVC, a view file has .cshtml extension.
A View is responsible for presenting a user interface. You can say it is the HTML part of a web application.
To add a View, right-click on the View folder and select Add->View.
Give a name to the View and click OK.
Another way to add a View –
In the controller class, right-click on an action method for which you want to add a View and select Add View.
View pages in ASP.Net Core MVC follow Razor syntaxes.
@{
    ViewData["Title"] = "Index";
}
<h2>Index</h2>

Model in ASP.Net Core MVC

The model in MVC represents domain-specific data. The model helps to maintain the data in an ASP.Net Core application. Model classes can be found in the Models folder in the ASP.Net Core MVC project structure.
To add a Model class in MVC, right-click on the Models folder within your project and select Add ->Class.

Run your ASP.Net Core MVC application

Press F5 to run your application. Add {Controller Name}/{Action Name} in the URL to see the output.
I hope you like this article.

Leave a Comment

RSS
YouTube
YouTube
Instagram