ASP.Net MVC框架配置与分析

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

ASP.Net MVC 框架配置与分析

 

 

前几年的时候ASP.Net 就有MVC 的概念,实际上就是我们熟悉的WebForm 开发模型。

1
Model 层与视图无关,并且只与数据库相关:
伪代码:
pulic class Model
{
      pulic DataSet ds()
      {
            //
数据库库取数据
            return ds;
      }
}

2
Controller 实际上就是我们熟悉的Default.aspx.cs 文件,继承自System.Web.UI.Page 类的文件
3
View 就是aspx 文件了

而今,微软推出了新的MVC 开发框架,也就是Microsoft ASP.NET 3.5 Extensions 。可以从这里下载:http://download.microsoft.com/download/6/5/c/65cad864-54a5-463a-9573-bc7d0bbc1df9/ASPNetExt.exe

当然,安装之前必须安装VS2008 。安装完后可以在创建项目的地方增加以下两种项目。

MVCProject.gif
一种是“ASP.NET MVC Web Application and Test” 会同时创建MVC 项目和一个测试项目,一个是ASP.NET MVC Web Application ,只会纯粹的建立MVC 项目。而在项目中引用新文件的时候,也会增加MVC 的页面。
MVCItem.gif

在创建MVC 项目以后,会增加以下文件
MVCPV.gif

访问的机制是通过URL 重写实现的。因此,你需要设置IIS 映射。 MVCIIS.gif


MVC
是通过HttpModule 进行地址重写的

  <httpModules>
   <add name=
"ScriptModule"  type= "System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
   <add name=
"DynamicDataModule"  type= "System.Web.DynamicData.DynamicDataHttpModule, System.Web.Extensions, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
   <add name=
"UrlRoutingModule"  type= "System.Web.Mvc.UrlRoutingModule, System.Web.Extensions, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  />
  </httpModules>


而在Global.asax.cs 中设置了映射关系
   //
设置Url 重写映射
   //URL
格式设置了访问的为controller( 这里是Home)action 对应HomeController 里的方法
   RouteTable.Routes.Add(new Route
   {
    Url = "[controller]/[action]/[id]",
    Defaults = new { action = "Index", id = (string)null },
    RouteHandler = typeof(MvcRouteHandler)
   });

    // 设置默认访问地址,因此主目录下看似无用的Default.aspx 不能删除掉
    //controller ="Home"
设置的是访问的路径,这里对应上图中的Home 目录,也对应了HomeController 的类名
   //action
是对应HomeController.cs 类中的Index 方法。
   RouteTable.Routes.Add(new Route
   {
    Url = "Default.aspx",
    Defaults = new { controller = "Home", action = "Index", id = (string)null },
    RouteHandler = typeof(MvcRouteHandler)
   });

HomeController.cs
代码:
 public class HomeController : Controller
 {
  [ControllerAction]
  public void Index()
  {
   RenderView("Index");
  }

  [ControllerAction]
  public void About()
  {
   RenderView("About");
  }
 }

HomeController 中找到要访问的页面

最后要说明的是Html.ActionLink("About Us", "About", "Test") 方法,能够实现自动链接处理,
参数1 是 链接显示的文字
参数2 是 要访问的文件
参数3 是 访问的目录,尽管参数名是controllerName.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值