Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET MVC Controller

ASP.NET MVC - Controller


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

Part 4: Add a controller.


The Controllers folder

The Controllers foldercontains the controller classes responsible for handling user input and responses.

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

In our example, Visual Web Developer has already created the following files: HomeController.cs(for the Home page and About page) andAccountController.cs (for the login page):

Controllers

Web servers typically map incoming URL requests directly to disk files on the server. For example, the URL request "http://www.w3cschool.cc/index.php" maps directly to the file "index.php" at the server root.

The MVC framework maps differently. MVC maps URLs to methods. These methods are called "controllers" in classes.

The controller is responsible for handling incoming requests, processing input, saving data, and sending responses back to the client.


The Home controller

The controller file in our applicationHomeController.csdefines two controls Index and About。

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 in the Views folder Index.cshtml and About.cshtml define the ActionResult views Index() and About() in the controller.