[译]剖析ASP.Net MVC Application

为了完全了解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。

Anatomy ASP.Net MVC Application5.创建一个默认的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”。

 Anatomy ASP.Net MVC Application

但是如果你尝试导航到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。现在你应该可以看到得到的响应结果跟之前的是一样的。

Anatomy ASP.Net MVC Application 

9.以一个View作为Index Action的返回值。更改Default Controller的Index Action的返回值为ActionResult。这里可以返回很多种类型的Result(比如JsonResult、ContentResult等等)。在本例中,我们将返回一个ViewResult。

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

Anatomy ASP.Net MVC Application

创建该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中添加进去的数据。

Anatomy ASP.Net MVC Application 

在这篇文章中,我从零开始创建了一个Asp.net MVC应用程序,用以剖析Asp.net MVC和了解在框架背后的神奇的东西。通过这可以帮助我在现有的Web Application中使用MVC。

原作者:
原文地址:Anatomy of an ASP.Net MVC Application
翻译:Inrie (洪晓军)

posted on 2008-12-23 14:38  Inrie  阅读(1539)  评论(2编辑  收藏  举报