views:

28

answers:

0

I'm building a Response Filter that will go through some tokens in an outputted page and replace them with other data. I would like to get the replacement data by calling an Action on a Controller. However, I'm having trouble accomplishing this. This question really comes down to how to instantiate a Controller and call an Action from outside code.

Here's the code I have so far:

            IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
            HttpRequest httpRequest = new HttpRequest(HttpContext.Current.Request.FilePath, HttpContext.Current.Request.Url.AbsoluteUri, string.Empty);
            MemoryStream memStream = new MemoryStream();
            HttpResponse response = new HttpResponse(new StreamWriter(memStream));
            HttpContextBase httpContext = new HttpContextWrapper(new HttpContext(httpRequest, response));
            RequestContext requestContext = new RequestContext(httpContext, new RouteData()
            {
                Values = { { "controller", "Home"}, { "action", "SomeAction" } }
            });
            IController controller = factory.CreateController(requestContext, "Home");
            controller.Execute(requestContext);

My thought was that this would output the View to the memStream MemoryStream and then I could read it from there. However, I'm getting the following exception:

The SessionStateTempDataProvider requires SessionState to be enabled.

at the last line (controller.Execute(requestContext))

How should I be doing this? Thanks!