Route、RouteDate 与 RouteTable
RouteDate
通过路由解析得到的路由数据
public class RouteData
{
public IDictionary<string, object> Values { get; private set; }
public IDictionary<string, object> DataTokens { get; private set; }
public IRouteHandler RouteHandler { get; set; }
public RouteBase Route { get; set; }
public RouteData()
{
this.Values = new Dictionary<string, object>();
this.DataTokens = new Dictionary<string, object>();
this.DataTokens.Add("namespaces", new List<string>());
}
public string Controller
{
get
{
object controllerName = string.Empty;
this.Values.TryGetValue("controller", out controllerName);
return controllerName.ToString();
}
}
public string ActionName
{
get
{
object actionName = string.Empty;
this.Values.TryGetValue("action", out actionName);
return actionName.ToString();
}
}
}
Route
路由对象或者叫路由模板
public class Route : RouteBase
{
public IRouteHandler RouteHandler { get; set; }
public string Url { get; set; }
public IDictionary<string, object> DataTokens { get; set; }
public Route()
{
this.DataTokens = new Dictionary<string, object>();
this.RouteHandler = new MvcRouteHandler();
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
IDictionary<string, object> variables;
if (this.Match(httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2), out variables))
{
RouteData routeData = new RouteData();
foreach (var item in variables)
{
routeData.Values.Add(item.Key, item.Value);
}
foreach (var item in DataTokens)
{
routeData.DataTokens.Add(item.Key, item.Value);
routeData.RouteHandler = this.RouteHandler;
return routeData;
}
}
return null;
}
public bool Match(string requestUrl, out IDictionary<string, object> variables)
{
variables = new Dictionary<string, object>();
string[] strArray1 = requestUrl.Split('/');
string[] strArray2 = this.Url.Split('/');
if (strArray1.Length!=strArray2.Length)
{
return false;
}
for (int index = 0; index < strArray2.Length; index++)
{
if (strArray2[index].StartsWith("{") && strArray2[index].EndsWith("}"))
{
variables.Add(strArray2[index].Trim("{ }".ToArray()), strArray1[index]);
}
else
{
if (string.Compare(strArray1[index], strArray2[index], true) != 0)
{
return false;
}
}
}
return true;
}
}
RouteTable
多个路由对象组成路由表
public class RouteTable
{
public static RouteDictionary Routes { get; private set; }
static RouteTable()
{
Routes = new RouteDictionary();
}
}
真正的大师永远怀着一颗学徒的心。

浙公网安备 33010602011771号