System.Web.Routing命名空间代码解析(一) RouteBase类,RouteData类,RouteValueDictionary类

1.RouteBase是一个抽象基类,定义了两个功能:

1.从HttpContextBase中根据Route Map规则得到RouteData

2.得到虚拟路径数据(将在 Route 类(下) 中讲解)

using  System.Security.Permissions;
using  System.Web;

namespace  System.Web.Routing
{
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level 
= AspNetHostingPermissionLevel.Minimal),
     AspNetHostingPermission(SecurityAction.LinkDemand, Level 
= AspNetHostingPermissionLevel.Minimal)]
    
public abstract class RouteBase
    
{
        
// Methods

        
public abstract RouteData GetRouteData(HttpContextBase httpContext);
        
public abstract VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values);
    }

}

 

2. RouteData类,保存Route数据的类,提供如下功能

     1.可保存对Route和RouteHandler的引用(基类和接口)

     2.保存两个键值对,分别存储RouteValues 和用户自传入的DataToken

     3.提供一个方法从RouteValues 中读取文本值

using  System;
using  System.Globalization;
using  System.Security.Permissions;
using  System.Web;

namespace  System.Web.Routing
{
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level 
= AspNetHostingPermissionLevel.Minimal),
     AspNetHostingPermission(SecurityAction.InheritanceDemand, Level 
= AspNetHostingPermissionLevel.Minimal)]
    
public class RouteData
    
{
        
// Fields
        private RouteValueDictionary _dataTokens;
        
private IRouteHandler _routeHandler;
        
private RouteValueDictionary _values;

        
// Methods
        public RouteData()
        
{
            
this._values = new RouteValueDictionary();
            
this._dataTokens = new RouteValueDictionary();
        }


        
public RouteData(RouteBase route, IRouteHandler routeHandler)
        
{
            
this._values = new RouteValueDictionary();
            
this._dataTokens = new RouteValueDictionary();
            
this.Route = route;
            
this.RouteHandler = routeHandler;
        }


        
public string GetRequiredString(string valueName)
        
{
            
object obj2;
            
if (this.Values.TryGetValue(valueName, out obj2))
            
{
                
string str = obj2 as string;
                
if (!string.IsNullOrEmpty(str))
                    
return str;
            }

            
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture,
                                                              RoutingResources.RouteData_RequiredValue,
                                                              
new object[] {valueName}));
        }


        
// Properties
        public RouteValueDictionary DataTokens
        
{
            
get return this._dataTokens; }
        }


        
public RouteBase Route getset; }

        
public IRouteHandler RouteHandler
        
{
            
get return this._routeHandler; }
            
set this._routeHandler = value; }
        }


        
public RouteValueDictionary Values
        
{
            
get return this._values; }
        }

    }

}

 

3.RouteValueDictionary 类,实现了一个IDictionary<stringobject>接口,包装了一个Dictionary<stringobject>对象,其实没什么功能的,唯一的功能就是支持传入一个对象(最好是匿名类型的对象),并通过反射把它当作键值对使用,详情请看代码中加粗的部分。

 

using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Linq;
using  System.Security.Permissions;
using  System.Web;

namespace  System.Web.Routing
{
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level 
= AspNetHostingPermissionLevel.Minimal),
     AspNetHostingPermission(SecurityAction.LinkDemand, Level 
= AspNetHostingPermissionLevel.Minimal)]
    
public class RouteValueDictionary : IDictionary<stringobject>
    
{
        
// Fields
        private Dictionary<stringobject> _dictionary;

        
Constructors

        
private void AddValues(object values)
        
{
            
if (values != null
)
                
foreach (PropertyDescriptor descriptor in
 TypeDescriptor.GetProperties(values))
                
{
                    
object obj2 =
 descriptor.GetValue(values);
                    
this
.Add(descriptor.Name, obj2);
                }

        }


        
public bool ContainsValue(object value)
        
{
            
return this._dictionary.ContainsValue(value);
        }


        
public Dictionary<stringobject>.Enumerator GetEnumerator()
        
{
            
return this._dictionary.GetEnumerator();
        }


        
public Dictionary<stringobject>.KeyCollection Keys
        
{
            
get return this._dictionary.Keys; }
        }


        
public Dictionary<stringobject>.ValueCollection Values
        
{
            
get return this._dictionary.Values; }
        }


        
IEnumerableKeyValuePairstring, object Members

        
IEnumerable Members

        
Implementation of ICollectionKeyValuePairstring,object

        
Implementation of IDictionarystring,object
    }

}

你可能感兴趣的:(System)