This section introduces
Use of ASP.NET MVC controller.To learn ASP.NET MVC, we will build an Internet application.
Part 4: Adding Controllers.
The Controllers folder contains control classes responsible for handling user input and responses.
MVC requires that all controller file names end with "Controller".
In our example, Visual Web Developer has created the following files: HomeController.cs (for the Home page and About page) and AccountController.cs (for the login page):
Web servers typically map incoming URL requests directly to disk files on the server. For example: the URL request "//www.w3cschool.cn/index.php" will be directly mapped to the file "index.php" on the server root directory.
MVC frameworks map differently. MVC maps URLs to methods. These methods are called "controllers" in the class.
The controller is responsible for handling incoming requests, processing input, saving data, and sending responses back to the client.
In the controller file HomeController.cs in our application, two controls Index and About are defined.
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();} } }
The files Index.cshtml and About.cshtml in the Views folder define the ActionResult views Index() and About() in the controller.