WEB开发网
开发学院WEB开发ASP.NET ASP.NET MVC学习笔记-Routing及视图引挚解析原理 阅读

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

 2009-11-20 16:52:33 来源:WEB开发网   
核心提示:首先打开项目的Global.asax.cs文件,我们将看到页面重写规则如下:view plaincopy to clipboardPRint?·········10········20········30········40········50········60········70········80········
首先打开项目的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;

Tags:ASP NET MVC

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接