ASP.NET MVC 早期版本 (Preview 2/3/RC) 在 Visual Studio 2008 中文版安裝後,**專案範本不會出現**,原因是安裝程式僅將範本檔案部署至英文版目錄 (`1033`),中文版目錄 (`2052`) 為空。本文記錄解決方法與初次建立專案的結構認識。
MVC 安裝程式將專案範本、項目範本、代碼片段複製至:
Visual Studio 2008 安裝目錄\Common7\IDE\ProjectTemplates\CSharp\Web\1033\ Visual Studio 2008 安裝目錄\Common7\IDE\ItemTemplates\CSharp\Web\1033\
中文版 VS 讀取 `2052` 目錄,故找不到範本。
%VSINSTALLDIR%\Common7\IDE\ProjectTemplates\CSharp\Web\1033\
%VSINSTALLDIR%\Common7\IDE\ProjectTemplates\CSharp\Web\2052\
devenv /installvstemplates
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
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);
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
<%@ 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 |
雖然早期 MVC 版本已成歷史,但其核心設計哲學 (關注點分離、可測試性、慣例優於配置) 至今仍是 ASP.NET Core 的基石。