top
Loading...
ASP.NET MVC 控製器

ASP.NET MVC - 控製器


為了學習 ASP.NET MVC,我們將構建一個 Internet 應用程序。

第 4 部分:添加控製器。


Controllers 文件夾

Controllers 文件夾包含負責處理用戶輸入和響應的控製類。

MVC 要求所有控製器文件的名稱以 "Controller" 結尾。

在我們的實例中,Visual Web Developer 已經創建好了以下文件: HomeController.cs(用於 Home 頁面和 About 頁面)和AccountController.cs (用於登錄頁面):

Web 服務器通常會將進入的 URL 請求直接映射到服務器上的磁盤文件。例如:URL 請求 "http://www.w3cschool.cc/index.asp" 將直接映射到服務器根目錄上的文件 "index.asp"。

MVC 框架的映射方式有所不同。MVC 將 URL 映射到方法。這些方法在類中被稱為"控製器"。

控製器負責處理進入的請求,處理輸入,保存數據,併把響應發送回客戶端。


Home 控製器

在我們應用程序中的控製器文件HomeController.cs,定義了兩個控件 IndexAbout

把 HomeController.cs 文件的內容替換成:

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 視圖

Views 文件夾中的文件 Index.cshtmlAbout.cshtml 定義了控製器中的 ActionResult 視圖 Index() 和 About()。


北斗有巢氏 有巢氏北斗