ASP.NET MVC学习笔记-Routing及视图引挚解析原理

王朝学院·作者佚名  2009-11-21  
宽屏版  字体: |||超大  

首先打开项目的Global.asax.cs文件,我们将看到页面重写规则如下:

view plaincopy to clipboardprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

public static void RegisterRoutes(RouteCollection routes)

{

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

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new { controller = "Home", action = "Index", id = "" } // Parameter defaults

);

}

protected void Application_Start()

{

RegisterRoutes(RouteTable.Routes);

}

public static void RegisterRoutes(RouteCollection routes)

{

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

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new { controller = "Home", action = "Index", id = "" } // Parameter defaults

);

}

protected void Application_Start()

{

RegisterRoutes(RouteTable.Routes);

}

MapRoute()重载如下:

view plaincopy to clipboardprint?

MapRoute( string name, string url);

MapRoute( string name, string url, object defaults);

MapRoute( string name, string url, string[] namespaces);

MapRoute( string name, string url, object defaults, object constraints);

MapRoute( string name, string url, object defaults, string[] namespaces);

MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

MapRoute( string name, string url);

MapRoute( string name, string url, object defaults);

MapRoute( string name, string url, string[] namespaces);

MapRoute( string name, string url, object defaults, object constraints);

MapRoute( string name, string url, object defaults, string[] namespaces);

MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

创建一个Route类实例,最关键的是为以下几个属性赋值:

属性名称 说明 举例

Constraints 获取或设置为 URL 参数指定有效值的表达式的词典。 {controller}/{action}/{id}

DataTokens 获取或设置传递到路由处理程序但未用于确定该路由是否匹配 URL 模式的自定义值。 new RouteValueDictionary { { "format", "short" } }

Defaults 获取或设置要在 URL 不包含所有参数时使用的值。 new { controller = "Home", action = "Index", id = "" }

RouteHandler 获取或设置处理路由请求的对象。 new MvcRouteHandler()

Url 获取或设置路由的 URL 模式。 new { controller = @"[^\.]*" }

Asp.net MVC会将url请求根据重写规则导向相应的controller中,然后在controller中调用相应的view()方法, 然后在ViewEngines中找到相匹配的ViewEngine查找并创建View实现页面解析,ASP.net MVC提供了VirtualPathProviderViewEngine实现了查找View的功能。

在IViewEngine中提供了母版及视图的路径格式字串的属性,格式如下:

MasterLocationFormats = new[] {

"~/Views/{1}/{0}.master",

"~/Views/Shared/{0}.master"

};

ViewLocationFormats = new[] {

"~/Views/{1}/{0}.aspx",

"~/Views/{1}/{0}.ascx",

"~/Views//Shared/{0}.aspx",

"~/Views/Shared/{0}.ascx"

};

PartialViewLocationFormats = ViewLocationFormats;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有