2】ASP.NETRouting路由对象模型的位置
3.】ASP.NETRouting路由对象模型的入口
4.】ASP.NETRouting路由对象模型的内部结构
4.1】UrlRoutingModule对象内部结构
|
1
2
3
4
|
protected
virtual
void
Init (HttpApplication application)
{
application.PostResolveRequestCache += PostResolveRequestCache;
}
|
|
1
2
3
4
5
|
void
PostResolveRequestCache (
object
o, EventArgs e)
{
var
app = (HttpApplication) o;
PostResolveRequestCache (
new
HttpContextWrapper (app.Context));
}
|
|
1
2
3
4
5
6
7
8
9
10
11
|
public
virtual
void
PostResolveRequestCache (HttpContextBase context)
{
var
rd = RouteCollection.GetRouteData (context);
//(1)匹配RouteData对象,后面分析;
var
rc =
new
RequestContext (context, rd);
//(2)封装计算出来的RouteData对象和当前HttpRequest对象;
IHttpHandler http = rd.RouteHandler.GetHttpHandler (rc);
//(3)使用(1)步骤计算出来的当前RouteData对象中的RouteHander属性获取路由处理程序IHttpHander接口;
context.Request.RequestContext = rc;
context.RemapHandler (http);
}
|
|
1
2
3
4
|
routes.MapRoute(
name:
"Default"
,
url:
"{controller}/{action}/{id}"
,
defaults:
new
{ controller =
"Home"
, action =
"Index"
, id = UrlParameter.Optional }
|
4.2】RouteBase、Route、RouteCollection、RouteTable路由核心对象模型
|
1
2
3
4
|
public
class
Route : RouteBase
{
public
IRouteHandler RouteHandler {
get
;
set
; }
}
|
|
1
2
3
4
|
public
interface
IRouteHandler
{
IHttpHandler GetHttpHandler (RequestContext requestContext);
}
|
|
1
|
public
override
RouteData GetRouteData (HttpContextBase httpContext)
|
|
1
2
3
4
5
6
7
8
|
public
void
Add (
string
name, RouteBase item)
{
lock
(GetWriteLock ()) {
base
.Add (item);
if
(!String.IsNullOrEmpty (name))
d.Add (name, item);
}
}
|
|
1
2
3
4
5
6
7
8
|
public
class
RouteTable
{
static
RouteTable ()
{
Routes =
new
RouteCollection ();
}
public
static
RouteCollection Routes {
get
;
private
set
; }
}
|
4.3】RouteValueDictionary、RouteData、RequestContext 路由数据对象模型
|
1
|
public
class
RouteValueDictionary : IDictionary<
string
,
object
>
|
|
1
|
Dictionary<
string
,
object
> d =
new
Dictionary<
string
,
object
> (CaseInsensitiveStringComparer.Instance);
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
internal
class
CaseInsensitiveStringComparer : IEqualityComparer<
string
>
{
public
static
readonly
CaseInsensitiveStringComparer Instance =
new
CaseInsensitiveStringComparer ();
public
int
GetHashCode (
string
obj)
{
return
obj.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
}
public
bool
Equals (
string
obj1,
string
obj2)
{
return
String.Equals (obj1, obj2, StringComparison.OrdinalIgnoreCase);
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public
RouteData (RouteBase route, IRouteHandler routeHandler)
{
// arguments can be null.
Route = route;
RouteHandler = routeHandler;
DataTokens =
new
RouteValueDictionary ();
Values =
new
RouteValueDictionary ();
}
public
RouteValueDictionary DataTokens {
get
;
private
set
; }
public
RouteBase Route {
get
;
set
; }
public
IRouteHandler RouteHandler {
get
;
set
; }
public
RouteValueDictionary Values {
get
;
private
set
; }
|
4.4】IRouteHandler 、IHttpHander两个接口之间的关系
5.】UrlRoutingHandler 对象内部结构及扩展应用
|
1
|
public
abstract
class
UrlRoutingHandler : IHttpHandler
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
protected
virtual
void
ProcessRequest (HttpContextBase httpContext)
{
if
(httpContext ==
null
)
throw
new
ArgumentNullException (
"httpContext"
);
var
rd = RouteCollection.GetRouteData (httpContext);
if
(rd ==
null
)
throw
new
HttpException (
"The incoming request does not match any route"
);
if
(rd.RouteHandler ==
null
)
throw
new
InvalidOperationException (
"No IRouteHandler is assigned to the selected route"
);
RequestContext rc =
new
RequestContext (httpContext, rd);
var
hh = rd.RouteHandler.GetHttpHandler (rc);
VerifyAndProcessRequest (hh, httpContext);
}
protected
abstract
void
VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext);
|
本文深入探讨了ASP.NET中Routing模块的核心对象及其工作原理,解析了如何在不影响现有框架的基础上提供良好的扩展性,并详细分析了Routing在ASP.NET管道模型中的位置、入口以及对象模型。文章还介绍了如何通过依赖注入接口实现自定义路由处理逻辑,以及如何在不同应用框架如ASP.NET MVC、ASP.NET Web API中灵活使用Routing系统。通过深入剖析Routing系统的关键组件和内部结构,旨在帮助开发者更好地理解和运用Routing模块,为构建更高效、轻量级的Web应用程序提供指导。






199

被折叠的 条评论
为什么被折叠?



