[转]剖析ASP.Net MVC Application

http://www.cnblogs.com/errorif/archive/2009/02/13/1389927.html

为了完全了解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 (洪晓军)

时间: 2024-08-08 01:26:35

[转]剖析ASP.Net MVC Application的相关文章

[转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10) By      

[转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application Download Completed Project The Contoso University sample web application de

Migrating an ASP.NET MVC application to ADFS authentication

I recently built an ASP.NET application at work to help track internal use of our products. It's been really well received, but only a few days after launch one of our managers came over and asked if we could move the site to Azure so that people did

CRUD Operations In ASP.NET MVC 5 Using ADO.NET

Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In GridView Using ASP.Net C#. It now has more than 140 K views, therefore to help beginners I decided to rewrite the article i with stepbystep approach u

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序读取相关数据

这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第六篇:为ASP.NET MVC应用程序读取相关数据 原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 译文版权所有,谢绝全文转载--但您可以在您的网站上添加到该教程的链接. 在之前的教程中您已经完成了学校数据模型.在本教程中你将

ASP.NET MVC过滤器(一)

MVC过滤器是加在 Controller 或 Action 上的一种 Attribute,通过过滤器,MVC 网站在处理用户请求时,可以处理一些附加的操作,如:用户权限验证.系统日志.异常处理.缓存等.MVC 中包含Authorization filter.Action filter.Result filter.Exception filter 四种过滤器. APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这

ASP.NET MVC WebGrid &ndash; Performing true AJAX pagination and sorting 【转】

ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebGrid is a very powerful HTML helper component introduced with ASP.NET MVC 3 and ASP.NET Web Pages. Despite all its cool features, it is troublesome to

ASP.NET MVC Display Mode 移动端视图 配置对微信内置浏览器的识别

最近在捣鼓一个稍微有点low的商城网站,没有计划做app却要求有个wap版,而前端又没有做成响应式,时间WTF,直接利用了asp.net mvc的Display Mode Provider. 使用方式依照上面的链接地址,asp.net mvc application启动的时候会在全局变量 DisplayModeProvider.Instance.Modes 集合中加入 DisplayModeId == "Mobile" 的 IDisplayMode ,因此如果想要在移动端浏览器中展示移

ASP.NET MVC和jQuery DataTable整合

本文包含代码示例说明如何jQuery插件开发者可以集成到ASP.NET MVC应用程序. 下载源代码- 87.4 KB Introduction The jQuery DataTables plug-in is an excellent client-side component that can be used to create rich-functional tables in the web browser. This plug-in adds lot of functionalitie