首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET MVC 入門實作:建立第一個 Controller 與 View 執行流程

     編輯:關於ASP.NET

前言

本文帶領從零建立 ASP.NET MVC 專案,新增第一個 EiceController,觀察預設產生代碼結構,並說明 Controller 基礎架構。

建立 Controller 步驟

  1. 方案總管右鍵 Controllers 資料夾 → Add → Controller。
  2. 命名為 EiceController(慣例以 Controller 結尾)。
  3. 選擇 MVC Controller Class 範本。

預設產生代碼分析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class EiceController : Controller
    {
        public void Index(string id)
        {
        }
    }
}

重點說明

  • 繼承 Controller 基類:所有 Controller 必須繼承 System.Web.Mvc.Controller。
  • Action 方法:public void Index(string id) 即為 Action,參數 id 會自動從路由資料繫結。
  • 命名空間:預設置於專案名稱.Controllers。

下一步

建立對應 View(Views/Eice/Index.aspx),並在 Action 中呼叫 RenderView("Index") 即可看到執行結果。後續將介紹 View 建立、資料傳遞、路由對應等核心概念。

結語

ASP.NET MVC 專案結構清晰,Controller 為核心進入點,理解其繼承體系與 Action 簽章是開發基礎。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved