find and solve ||
Please wait.....

Getting Started with ASP.NET Core with C#

Getting Started with ASP.NET Core with C#

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

What is the MVC pattern?

(MVC) Model-View-Controller architectural pattern separates an application into three main groups of components like  Models, Views, and Controllers. When using this pattern, user requests are routed to first a Controller which is responsible for working on the Model to perform user actions and/or retrieve results of queries
The given diagram shows the three main components Model, View,Controller, and which ones reference the others:

What is the MVC pattern?

Controller Responsibilities

Controller are Interacts with Model and View and also andle user interaction, work with the model, and ultimately select a view to render.The view only displays information in an MVC application.Controller are the mediator of View and Model.

"Controllers shouldn't be overly complicated by too many responsibilities. To keep controller logic from becoming overly complex, push business logic out of the controller and into the domain model."

Model Responsibilities

The Model in an MVC application represents entity class model,the state of the application and any business logic or operations that should be performed by it.In the mode,Business logic should be encapsulated along with any implementation logic for persisting the state of the application.

View Responsibilities

Views are responsible for presenting content through the user interface and use Razor view engine also you can use .aspx.we can use C# code in view page start with "@" systax.Run time Razor systax .cs code covert into html markup.If you want to need  perform a great deal of logic in view files in order to display data from a complex model and consider the using a View Component, ViewModel, or view template to simplify the view.

What is ASP.NET Core MVC

This framework is an open-source, lightweight, and highly testable presentation framework optimized for use with ASP.NET Core.Build dynamic websites and application that enables a clean separation of concerns by patterns-based. Asp.Net core MVC  provides a patterns-based way to build dynamic applications. It gives full control over markup and supports TDD-friendly development and also uses the latest web standards.ASP.NET Core Build view page as View.dll.

Benefits of using views

⦁    The app is easier to maintain because it's better organized. Views are generally grouped by app feature. It is easier to find related views when working on a feature.
⦁    The parts of the app are loosely coupled. We can update and build the app's views separately from the business logic and data access components. We can modify the views of the app without necessarily having to update other parts of the app.
⦁    It's easier to test the user interface parts of the app because the views are separate units.
⦁    Due to better organization, it's less likely that you'll accidentally repeat sections of the user interface.

For Example Razor view engine

    @for (int f = 0; f < 8; f++) {
OR  @model IEnumerable<Staff> <ul>     @foreach (Staff p in Model)     {         <li>@p.Name</li>     } </ul> <Staff>:- is the class we can create class inside the Model folder. @model IEnumerable<Staff> :-This is the ViewModel Path


Model binding

ASP.NET Core MVC model binding converts client request data (form route data,query string parameters, HTTP headers,values) into objects that the controller can handle it.

public async Task<IActionResult> StaffList(StaffViewModel model, string returnUrl = null) { ... }

Model validation

using System.ComponentModel.DataAnnotations;
public class StaffViewModel {     [Required]     [EmailAddress]     public string Email { get; set; }     [Required]     [DataType(DataType.Currency)]     public string Salary { get; set; }     [Display(Name = "Join Date")]     public DateTime JoinDate { get; set; }    }

A controller action: 

public async Task<IActionResult> Satff(StaffViewModel model, string returnUrl = null)
    if (ModelState.IsValid)
        // work with the model
    // At this point, something failed, redisplay form
    return View(model);

Getting started with ASP.NET Core MVC in C# involves the following steps:

  1. Install the .NET Core SDK and Visual Studio or another code editor.
  2. Create a new ASP.NET Core Web Application project in Visual Studio or using the .NET Core CLI.
  3. Choose the "ASP.NET Core Web Application" template and select the "Web Application (Model-View-Controller)" option.
  4. Add controllers, models, and views to define the application's behavior, data, and presentation.
  5. Use Razor pages to create dynamic web pages that display information from the model and respond to user interactions.
  6. Test the application using the built-in web server or deploy it to a production environment.

Here's a simple example of a Controller in ASP.NET Core MVC:

using Microsoft.AspNetCore.Mvc; namespace MyWebApp.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); } } }

And here's a simple example of a View in ASP.NET Core MVC:

<h1>Welcome to My Web App</h1>

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If felt valuable to you, feel free to share it.


Report Response