国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net mvc路由篇 如何找到 IHttpHandler方法介紹

2024-07-10 12:41:19
字體:
來源:轉載
供稿:網友
學習是使用asp.net已經有很長一段時間了,現在就來分析一下mvc的整過過程吧。個人計劃寫一個mvc系列的博文,僅從源代碼的角度來分析mvc。在接觸mvc時我們一定會經歷路由,那么路由這東東是怎么搞出來的啊。在我們的web.config中有這么一句: <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 看來路由是它咋負責了。在這個dll中有一個很特殊的類UrlRoutingModule
我們來看看它里面主要的核心代碼吧:
代碼如下:
protected virtual void Init(HttpApplication application)
{
if (application.Context.Items[_contextKey] == null)
{
application.Context.Items[_contextKey] = _contextKey;
application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
}
}

private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
this.PostResolveRequestCache(context);
}

public virtual void PostResolveRequestCache(HttpContextBase context)
{
RouteData routeData = this.RouteCollection.GetRouteData(context);
if (routeData != null)
{
IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[0]));
}
if (!(routeHandler is StopRoutingHandler))
{
RequestContext requestContext = new RequestContext(context, routeData);
context.Request.RequestContext = requestContext;
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("UrlRoutingModule_NoHttpHandler"), new object[] { routeHandler.GetType() }));
}
if (httpHandler is UrlAuthFailureHandler)
{
if (!FormsAuthenticationModule.FormsAuthRequired)
{
throw new HttpException(0x191, SR.GetString("Assess_Denied_Description3"));
}
UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this);
}
else
{
context.RemapHandler(httpHandler);
}
}
}
}

在IHttpModule.Init中注冊了一個PostResolveRequestCache事件,而該事件主要是調用PostResolveRequestCache這個方法,在這個方法里面有幾句很重要的代碼是
代碼如下:
RouteData routeData = this.RouteCollection.GetRouteData(context);
IRouteHandler routeHandler = routeData.RouteHandler;
RequestContext requestContext = new RequestContext(context, routeData);
context.Request.RequestContext = requestContext;
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
context.RemapHandler(httpHandler);

讓我們來分析第一句RouteData routeData = this.RouteCollection.GetRouteData(context) ,這句我們猜測是獲取路由信息。要想理解這句代碼又得回到我們程序中來,我們在Global.asax.cs文件中的RegisterRoutes方法中,默認有這么一句
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开远市| 莲花县| 彰武县| 河北区| 余干县| 平谷区| 祁阳县| 海宁市| 山阴县| 漳平市| 寿阳县| 扶绥县| 修武县| 手游| 房山区| 怀宁县| 白银市| 镇雄县| 云阳县| 砚山县| 徐水县| 镇原县| 伊宁县| 青川县| 东方市| 新邵县| 桓台县| 平塘县| 策勒县| 乌恰县| 定襄县| 高淳县| 南岸区| 双鸭山市| 依安县| 同德县| 康平县| 靖江市| 富顺县| 唐海县| 前郭尔|