Example #1
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            foreach (var param in requestContext.RouteData.Values)
            {
                requestContext.HttpContext.Items[param.Key] = param.Value;
            }



            // Create Controller Instance
            IController controller = GetControllerFromRouteValues(requestContext);

            if (controller == null)
            {
                return(null);
            }

            // Call Initialize on the Controller
            controller.Initialize(requestContext);


            // Call action on the controller
            var actionResult = CallActionOnControllerAndReturnActionName(requestContext, controller) as ActionResult;

            actionResult.requestContext = requestContext;


            // Create page from BuildManager using VirtualPath of the physical aspx file
            // /Views/{controller}/{action}.aspx
            string virtualPath = String.Format("~/Views/{0}/{1}.aspx", requestContext.RouteData.Values["controller"], requestContext.RouteData.Values["action"]);

            IHttpHandler handler = System.Web.HttpContext.Current.CurrentHandler;

            switch (actionResult.GetType().Name)
            {
            case "ViewResult": {
                var page     = ((ViewResult)actionResult).CreateView(virtualPath);
                var viewData = page.GetPropertyValue("ViewData");
                SetupViewPage(controller, page, viewData);
                handler = page as IHttpHandler;
                break;
            }

            case "XmlResult": {
                handler = new XMLResponseHandler((actionResult as XmlResult).Xml);
                break;
            }

            case "JsonResult": {
                handler = new JsonResponseHandler((actionResult as JsonResult).Json);
                break;
            }

            default:
                break;
            }


            return(handler as IHttpHandler);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            foreach (var param in requestContext.RouteData.Values) {
                requestContext.HttpContext.Items[param.Key] = param.Value;
            }

            // Create Controller Instance
            IController controller = GetControllerFromRouteValues(requestContext);

            if (controller == null) return null;

            // Call Initialize on the Controller
            controller.Initialize(requestContext);

            // Call action on the controller
            var actionResult = CallActionOnControllerAndReturnActionName(requestContext, controller) as ActionResult;
            actionResult.requestContext = requestContext;

            // Create page from BuildManager using VirtualPath of the physical aspx file
            // /Views/{controller}/{action}.aspx
            string virtualPath = String.Format("~/Views/{0}/{1}.aspx", requestContext.RouteData.Values["controller"], requestContext.RouteData.Values["action"]);

            IHttpHandler handler = System.Web.HttpContext.Current.CurrentHandler;
            switch (actionResult.GetType().Name) {

                case "ViewResult": {
                    var page = ((ViewResult)actionResult).CreateView(virtualPath);
                    var viewData = page.GetPropertyValue("ViewData");
                    SetupViewPage(controller, page, viewData);
                    handler = page as IHttpHandler;
                    break;
                }

                case "XmlResult": {
                    handler = new XMLResponseHandler((actionResult as XmlResult).Xml);
                    break;
                }

                case "JsonResult": {
                    handler = new JsonResponseHandler((actionResult as JsonResult).Json);
                    break;
                }

                default:
                    break;

            }

            return handler as IHttpHandler;
        }