Razor Tutoriallogin
Razor Tutorial
author:php.cn  update time:2022-04-11 14:21:21

MVC controller


ASP.NET MVC - Controller


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

Part 4: Adding Controllers.


Controllers folder

Controllers folder Contains control classes responsible for handling user input and responses.

MVC requires that the names of all controller files 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 login pages):

pic_mvc_controllers.jpg

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" will be mapped directly 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.


Home Controller

The controller file HomeController.cs in our application defines 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

Files in the Views folder Index.cshtml and About.cshtml define the ActionResult views Index() and About() in the controller.


php.cn