Пример #1
0
        internal IHttpHandler GetHttpHandler(HttpContext context, UrlActionInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }


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

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


            // 创建能够调用Action的HttpHandler
            return(ActionHandlerFactory.CreateHandler(vkInfo));
        }
        /// <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));
        }
        IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context,
                                                    string requestType, string virtualPath, string physicalPath)
        {
            // 说明:这里不使用virtualPath变量,因为不同的配置,这个变量的值会不一样。
            // 例如:/mvc/*/*.aspx 和 /mvc/*
            // 为了映射HTTP处理器,下面直接使用context.Request.Path

            string requestPath = context.Request.Path;
            string vPath       = context.GetRealVirtualPath();

            // 尝试根据请求路径获取Action
            ControllerResolver controllerResolver = new ControllerResolver(context);
            InvokeInfo         vkInfo             = controllerResolver.GetActionInvokeInfo(vPath);

            // 如果没有找到合适的Action,并且请求的是一个ASPX页面,则按ASP.NET默认的方式来继续处理
            if (vkInfo == null)
            {
                if (requestPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
                {
                    // 调用ASP.NET默认的Page处理器工厂来处理
                    try {
                        return(_msPageHandlerFactory.GetHandler(context, requestType, requestPath, physicalPath));
                    }
                    catch (Exception ex) {
                        if (controllerResolver.DiagnoseResult != null)
                        {
                            controllerResolver.DiagnoseResult.ErrorMessages.Add("System.Web.UI.PageHandlerFactory不能根据指定的URL地址创建IHttpHandler实例。");
                            controllerResolver.DiagnoseResult.ErrorMessages.Add(ex.Message);

                            return(Http404DebugModule.TryGetHttp404PageHandler(context));
                        }

                        throw;
                    }
                }
            }

            return(ActionHandlerFactory.CreateHandler(vkInfo));
        }