[譯]剖析ASP.Net MVC Application

來源:互聯網
上載者:User

為了完全瞭解Asp.net MVC是怎樣工作的,我將從零開始建立一個MVC應用程式。

1.建立一個新的ASP.Net Web Application。它包括有一個Default.aspx頁面,一個標準的web.config檔案和添加一些初始的引用。

2.添加對“System.Web.Abstractions.dll”、“System.Web.Routing.dll” 和“System.Web.Mvc.dll”的引用,所有的這些都可以在“c:\Program Files\Microsoft ASP.NET\ASP.NET MVC Beta\Assemblies”檔案夾中找到(譯註:即Asp.net MVC的安裝路徑)。

使用MvcHttpHandler來處理MVC請求。開啟Default.aspx的Code-behind檔案(Default.aspx.cs)。在Page_Load方法中,以MVC的方式來處理請求。

protected void Page_Load(object sender, EventArgs e)
{
  HttpContext.Current.RewritePath(Request.ApplicationPath);
  IHttpHandler httpHandler = new MvcHttpHandler();
  httpHandler.ProcessRequest(HttpContext.Current);
}
 

3.添加一個全域應用程式類(global.asax)。在Application_Start方法中,映射Route到Home Controller。

protected void Application_Start(object sender, EventArgs e)
{
  RouteTable.Routes.MapRoute("Default Route",
    "{controller}/{action}",
    new { controller = "Default", action="Index" });
}
 

4.為了使用MapRoute和IgnoreRoute方法,你必須先using System.Web.Mvc命名空間(因為它們是擴充方法)。MapRoute方法以route的名字作為第一個參數,以URI模板為第二個參數(譯註:如"{controller}/{action}"),以預設值為第三個參數。應該注意到,預設值對象應該有屬性來匹配URI模板中的屬性的(譯註:如上面代碼中,預設值對象有兩個屬性:controller和action,用來匹配URI模板中的{controller}和{action})。上面的route映射一個Url到Contoller和Action。

5.建立一個預設的Controller。在Web Application的Controllers檔案夾中建立一個類,命名為DefaultController。注意到這裡的命名規範;Default是route的預設值,而"Controller"只是命名規範中約定的一個尾碼名。

Controller類應該繼承自System.Web.Mvc.Controller,同時應該包括一個public的方法作為Action。因為預設的Action是Index(從預設的route中可以看出),這個類應該看起來像這樣:

public class DefaultController : Controller
{
  public string Index() 
  {
    return "Hello, world";
  }
}
 

6.運行應用程式,導航到應用程式的目錄(“/”),你可以看到得到的響應是“hello,world”。

 

但是如果你嘗試導航到Default Controller的Index Action(/Default/Index)時,將會得到一個錯誤資訊。

7.添加Url Routing Module.開啟web.config,定位到<system.web>下的<httpModules>,添加Url Routing Module:

<httpModules>
  ...  
<add name="UrlRoutingModule"
       type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, 
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
 

8.運行該應用程式,導航到Default控制器的Index Action。現在你應該可以看到得到的響應結果跟之前的是一樣的。

 

9.以一個View作為Index Action的傳回值。更改Default Controller的Index Action的傳回值為ActionResult。這裡可以返回很多種類型的Result(比如JsonResult、ContentResult等等)。在本例中,我們將返回一個ViewResult。

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

建立該Action對應的View頁面。當調用一個無參的View()方法時,它會在跟Controller同名的檔案夾中尋找跟Action方法同名的View頁面。現在在Views\Default\檔案夾下建立一個新的頁面:Index.aspx。

為了讓該頁面成為MVC的View,開啟code behind的檔案(Index.aspx.cs),讓該類繼承自System.Web.Mvc.ViewPage。

修改該頁面(在Design模式或者Source模式),添加問候資訊:

<body>
  <form id="form1" runat="server">
    <div>
      <h1>Hello, world</h1>
    </div>
  </form>
</body>
 

10. 運行該應用程式,你應該可以接收到從我們剛剛建立的View頁面中發出的響應資訊。Routing引擎調用Default Controller的Index Action,返回View頁面(Index.aspx)。

11. 顯示View頁面的資料。開啟Controller,找到Index方法,添加資料到ViewData字典中。

public ActionResult Index()
{
  ViewData["name"] = "Guy";
  return View();
}

現在,在View頁面,在問候語那行中使用該資料。

<body>
  <form id="form1" runat="server">
    <div>
      <h1>Hello, <%= ViewData["name"] %></h1>
    </div>
  </form>
</body>


12.運行應用程式,可以看到在Controller中添加進去的資料。

 

在這篇文章中,我從零開始建立了一個Asp.net MVC應用程式,用以剖析Asp.net MVC和瞭解在架構背後的神奇的東西。通過這可以協助我在現有的Web Application中使用MVC。

原作者:Guy Burstein
原文地址:Anatomy of an ASP.Net MVC Application
翻譯:Inrie (洪曉軍)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.