ASP.NET MVC URL Routing

<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @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:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 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;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {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:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* 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;} /* List Definitions */ @list l0 {mso-list-id:1332099310; mso-list-type:hybrid; mso-list-template-ids:1738587898 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:32.25pt; mso-level-number-position:left; margin-left:32.25pt; text-indent:-21.0pt; font-family:Wingdings;} @list l1 {mso-list-id:1761288621; mso-list-type:hybrid; mso-list-template-ids:1198972508 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l1:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:32.25pt; mso-level-number-position:left; margin-left:32.25pt; text-indent:-21.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

URL Routing

ASP.NET MVC 组件协作

组件协作

 

URL Routing 组件   (System.Web.Routing.dll )

 

ASP.NET 中的作用

l         ASP.NET MVC 使用 URL 进行驱动

l         根据程序中制定的规则从 URL

--- 确定 Controller

--- 确定 Action

--- 生成其余各种参数

 

Route Class

l         URL

{Controller}/{action}/{id}

l         Defaults

默认   action=”Index”,id=””  当没有指定 action Id 时,系统将使用默认值

l         Constraints

约束:主要使用正则表达式来定义约束

如: year=”/d{4}”

还可以使用指定 httpMethod 的方式

如: httpMethod=”POST”

l         DataTokens

l         Route Handler

 

定义 Route 规则

            注意: 在使用 Route 规则之前,必须在 Web.config 文件中配置这个节点,一般在建立 MVC Web 应用程序是都默认配置了。

< httpModules >

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

</ httpModules >

Asp.net 应用程序中,我们将在 Global.asax 文件中定义 Route 规则。

            如:

                protected void Application_Start()

        {

            /// 在程序启动的时候注册我们定义的Route 规则

            RegisterRoutes(RouteTable .Routes);

        }

                public static void RegisterRoutes(RouteCollection routes)

        {

            // 忽略对.axd 文件的Route ,也就是和WebForm 一样直接去访问.axd 文件

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}" );

 

            routes.MapRoute(

                "Admin" ,                             // 规则名

                "Admin/{action}/{id}" ,       //Url

                new { controller = "Admin" , action = "Index" , id = "" }     //Default

            );

                         

                        routes.MapRoute(

                "PostById" ,

                "Post/{id}" ,

                new { controller = "Home" , action = "Post" , id = "" },

         new { id = @"[/da-f]{8}-[/da-f]{4}-[/da-f]{4}-[/da-f]{4}-[/da-f]{12}" }

             );

                    }

              以上是一个Global.ascx 文件,在这个文件中,我定义了两个ruote 规则。一个Route 规则,最重要的一个事URL, 另外一个就是默认值。默认值是很重要的。从上面的例子中,我们可以很容易看出Route 规则的一般格式:

        Routes.MapRoute{  规则名

URL ”,

Default,

Constraint 约束 };

  首先,指定的是规则名称,用于指定规则名的调用中。

      URL ,这就是我们要定义的 Route, 也就是调用规则。定义如下:

              {controller/action/id}

如我们要调用 Views Admin 文件夹下的 Details.aspx 文件,我们可以在浏览器地址栏中这样写   http://localhost:3694/Admin/Details 3694 是端口号:这个可以自己指定,在 Web 应用程序的“属性”,“ Web ”节点中配置。

      Default: 指定,在默认情况下的的 URL 规则。

      如: new { controller = "Home" , action = "Post" , id = "" } ,这是使用的是.NET3.0 中的匿名类。

      Constraint: 约束:定义 URL 的规则,根据需要写规则。如:

      New { controller= @ 正则表达式 ,id= @ 正则表达式 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Daniel的技术博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值