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

初探 ASP.NET MVC 框架:VS2008 中文版安裝模板缺失問題解決與專案結構認識

     編輯:關於ASP.NET

ASP.NET MVC 早期版本 (Preview 2/3/RC) 在 Visual Studio 2008 中文版安裝後,**專案範本不會出現**,原因是安裝程式僅將範本檔案部署至英文版目錄 (`1033`),中文版目錄 (`2052`) 為空。本文記錄解決方法與初次建立專案的結構認識。

問題現象

  • 執行 `AspNetMVCPreview2-setup.msi` 安裝完成
  • VS2008 「新增專案」對話框中**找不到** "ASP.NET MVC Web Application" 範本
  • 英文版 VS2008 正常顯示範本

根因

MVC 安裝程式將專案範本、項目範本、代碼片段複製至:

Visual Studio 2008 安裝目錄\Common7\IDE\ProjectTemplates\CSharp\Web\1033\
Visual Studio 2008 安裝目錄\Common7\IDE\ItemTemplates\CSharp\Web\1033\

中文版 VS 讀取 `2052` 目錄,故找不到範本。

解決方法:複製範本至中文目錄

  1. 關閉所有 Visual Studio 實例
  2. 開啟檔案總管,前往:
    %VSINSTALLDIR%\Common7\IDE\ProjectTemplates\CSharp\Web\1033\
            
  3. 複製全部 `.zip` 範本檔案至:
    %VSINSTALLDIR%\Common7\IDE\ProjectTemplates\CSharp\Web\2052\
            
  4. 同樣處理 `ItemTemplates\CSharp\Web\1033\` → `2052\`
  5. 以系統管理員身分執行「Visual Studio 2008 命令提示字元」,輸入:
    devenv /installvstemplates
            
  6. 重新開啟 VS2008,「新增專案」即可看到 **ASP.NET MVC Web Application**

建立專案後的預設結構 (Preview 2/3/RC 版本)

MvcApplication/
├── Controllers/
│   ├── HomeController.cs
│   └── AccountController.cs
├── Models/
│   └── (空,待加入領域模型)
├── Views/
│   ├── Home/
│   │   ├── Index.aspx
│   │   └── About.aspx
│   ├── Account/
│   │   ├── Login.aspx
│   │   └── Register.aspx
│   └── Shared/
│       ├── Site.Master          # 母版頁 (Layout)
│       └── Error.aspx
├── Content/
│   └── Site.css
├── Scripts/
│   ├── jquery-1.2.6.js
│   └── MicrosoftMvcAjax.js
├── App_Data/
├── Global.asax.cs              # 路由註冊、應用程式事件
├── Web.config
└── MvcApplication.csproj

關鍵檔案解析

Global.asax.cs - 路由註冊核心


public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // 路由名稱
            "{controller}/{action}/{id}",                           // URL 模式
            new { controller = "Home", action = "Index", id = "" }  // 預設值
        );
    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

HomeController.cs - 控制器範例


public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

Views/Home/Index.aspx - 視圖 (WebFormViewEngine)


<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
         Inherits="System.Web.Mvc.ViewPage" %>


    

<%= ViewData["Message"] %>

早期 MVC 使用 `.aspx` 視圖引擎 (WebFormViewEngine),語法與 Web Forms 相似但**無 Code-behind、無 ViewState、無 Server Controls**。

後續版本演進重點

版本關鍵變更
MVC 1.0 (2009)正式版、支援 VS2008 SP1、WebFormViewEngine 為主
MVC 2 (2010)區域、強型別 HtmlHelper、驗證改進、Templated Helpers
MVC 3 (2011)**Razor 視圖引擎**、Dependency Resolver、Global Filters、JSON 繫結
MVC 4 (2012)Web API、行動裝置專案範本、Bundling/Minification、OAuth
MVC 5 (2013)Identity、Attribute Routing、Filter Overrides、Bootstrap 預設
Core MVC (2016~)跨平台、Middleware、內建 DI、Endpoint Routing、Razor Pages

學習建議

  1. 理解 **路由 → Controller → Action → View** 基本流程
  2. 掌握 **ViewData / ViewBag / TempData** 傳遞資料差異
  3. 學習 **HtmlHelper** (`Html.TextBoxFor`, `Html.ValidationMessageFor` 等) 產生表單
  4. 認識 **Model Binding** 如何將 Form/QueryString 映射到 Action 參數
  5. 實作 **Repository/Service** 模式,Controller 僅協調流程
  6. **新專案直接使用 ASP.NET Core MVC**,舊專案維護才需回顧早期版本細節

雖然早期 MVC 版本已成歷史,但其核心設計哲學 (關注點分離、可測試性、慣例優於配置) 至今仍是 ASP.NET Core 的基石。

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