Ejemplo n.º 1
0
        void app_PostResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication app     = (HttpApplication)sender;
            HttpContext     context = app.Context;

            // 如果已经被处理过了,就不再处理。
            if (context.Handler != null)
            {
                return;
            }

            string virtualPath = context.GetRealVirtualPath();



            // 检查有没有匹配的路由规则
            RouteData routeData = GetRoute(context, virtualPath);

            if (routeData == null)
            {
                return;                         // 没有就忽略,重新交给ASP.NET管线继续处理。
            }
            MvcRouteHandler routeHandler = routeData.RouteHandler as MvcRouteHandler;

            if (routeHandler == null)
            {
                //return;	// 忽略就会产生404错误了。因此为了排查方便,抛出异常。
                throw new InvalidProgramException("使用ClownFish.Mvc.MvcRoutingModule时,必须指定MvcRouteHandler");
            }


            UrlActionInfo info = routeHandler.GetUrlActionInfo(routeData, context);

            info.SetHttpcontext(context);


            IHttpHandler handler = routeHandler.GetHttpHandler(context, info);

            if (handler != null)
            {
                context.RemapHandler(handler);
            }
            else
            {
                ExceptionHelper.Throw404Exception(context);
            }
        }
        /// <summary>
        /// 实现IHttpHandlerFactory接口,从当前请求获取IHttpHandler
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="virtualPath"></param>
        /// <param name="physicalPath"></param>
        /// <returns></returns>
        public IHttpHandler GetHandler(HttpContext context,
                                       string requestType, string virtualPath, string physicalPath)
        {
            // 说明:这里不使用virtualPath变量,因为不同的配置,这个变量的值会不一样。
            // 例如:/Ajax/*/*.aspx 和 /Ajax/*
            // 为了映射HTTP处理器,下面直接使用context.Request.Path

            string vPath = context.GetRealVirtualPath();



            // 根据请求路径,定位到要执行的Action
            UrlActionInfo info = ParseUrl(context, vPath);

            if (info == null)
            {
                IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context);
                if (handler != null)
                {
                    return(handler);
                }

                ExceptionHelper.Throw404Exception(context);
            }

            info.SetHttpcontext(context);



            // 获取内部表示的调用信息
            ControllerResolver controllerResolver = new ControllerResolver(context);
            InvokeInfo         vkInfo             = controllerResolver.GetActionInvokeInfo(info);

            if (vkInfo == null)
            {
                IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context);
                if (handler != null)
                {
                    return(handler);
                }

                ExceptionHelper.Throw404Exception(context);
            }

            // 创建能够调用Action的HttpHandler
            return(ActionHandlerFactory.CreateHandler(vkInfo));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 实现IRouteHandler接口
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            HttpContext context = requestContext.HttpContext.ApplicationInstance.Context;

            UrlActionInfo info = GetUrlActionInfo(requestContext.RouteData, context);

            if (info == null)
            {
                IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context);
                if (handler != null)
                {
                    return(handler);
                }

                ExceptionHelper.Throw404Exception(context);
            }

            info.SetHttpcontext(context);

            return(GetHttpHandler(context, info));
        }