ASP.NET MVC - Controllers
- Previous Page MVC Layout
- Next Page MVC View
To learn ASP.NET MVC, we will build an Internet application.
Part 4:Add a controller.
Controllers folder
The Controllers folder contains controller classes responsible for handling user input and responses.
MVC requires that all controller names must end with "Controller".
In our example, Visual Web Developer has created the following files:HomeController.cs(For home page and about page) and AccountController.cs (For login page):
The web server usually maps incoming URL requests directly to disk files on the server. For example: a URL request (such as "http://www.codew3c.com/index.asp") is mapped to the file "index.asp" in the root directory of the server.
The mapping method of the MVC framework is different. 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.
Home Controller
Controller files in our application HomeController.csDefine two controls Index and About.
Replace the content 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
Files in the Views folder Index.cshtml and About.cshtml Defined ActionResult views Index() and About() in the controller.
- Previous Page MVC Layout
- Next Page MVC View