Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET MVC controller


May 12, 2021 ASP.NET


Table of contents


ASP.NET MVC - Controller

This section describes ASP.NET the use of MVC controllers.

To learn ASP.NET MVC, we'll build an Internet application.

Part 4: Add a controller.


Controllers folder

The Controllers folder contains control classes that handle user input and responses.

MVC requires the name of all controller files to end with "Controller".

In our example, Visual Web Developer has created files: HomeController .cs (for Home and About pages) and AccountController .cs (for login pages):

ASP.NET MVC controller

Web servers typically map in-url requests directly to disk files on the server. For example, the URL request "/www.w3cschool.cn/index.php" maps directly to the file "index" on the .php.

The MVC framework is mapped differently. M VC maps the URL to the method. These methods are called "controllers" in classes.

The controller handles in-in requests, processes inputs, saves data, and sends responses back to the client.


Home controller

HomeController, the controller file in .cs, defines two controls, Index and Out.

Replace the contents of the HomeController .cs file with:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{return View();}

public ActionResult About()
{return View();}
}
}


Controller view

The files index.cshtml and About.cshtml in the Views folder define ActionResult View Index() and About() in the controller.