align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditemptyheartfacebookfullheartglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

Re: [dotnet-120] Calling MVC Action from outside a web request

From: Noah B.
Sent on: Wednesday, September 16, 2009 10:24 AM
Well, I was hoping someone had some experience with this...

In case anyone is wondering, here's what I came up with:

First of all, instead of using the IControllerFactory, I switched to just using reflection to create an instance of my controller.  This has the advantage of requiring a lot less wire-up, so with my proof of concept I have the following code:

            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));
            HomeController homeController = new HomeController();
            ControllerContext controllerContext = new ControllerContext(httpContext, new RouteData(){
                Values = {{ "controller", "Home"}, { "action", "SomeAction"}}
            }, homeController);          
            ActionResult result = homeController.SomeAction();
            result.ExecuteResult(controllerContext);

Now here's the trick: you might think that now if you run response.Flush you'll have the output from the ActionResult spit out into the memStream object.  But you'd be wrong!  This really threw me off, but I found the culprit:

ViewPage.cs in System.Web.Mvc (starting line 110):

        public virtual void RenderView(ViewContext viewContext) {
            ViewContext = viewContext;
            InitHelpers();
            // Tracing requires Page IDs to be unique.
            ID = Guid.NewGuid().ToString();
            ProcessRequest(HttpContext.Current);
        }

So the ViewPage is passing the HttpContext.Current to its parent's (Page) ProcessRequest method.  So, you'll always be rendering out to the Stream on HttpContext.Current!

- Noah

Darshan Mehta wrote:
Have you tried looking at the ASP.NET MVC Source Code?
It might have an answer to your question.

Darshan
"Friends are angels who lift us to our feet when
our wings have trouble remembering how to fly."
 
Beta Tester Badge 2



 

From: [address removed]
To: [address removed]
Subject: [dotnet-120] Calling MVC Action from outside a web request
Date: Tue, 15 Sep[masked]:19:47 -0400

I'm working on a Request Filter for an application I'm writing that basically goes through and replaces certain tokens with real data.  All this currently happens inside the Filter (Stream's) Flush method, but I want to refactor it to take advantage of some existing Controller Actions.

I wrote the following code (which doesn't work, exception below):

            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);

I figured that the ActionResult would be written to the memStream and then I could take it from there.  However, I'm getting the following exception (on the last line):

The SessionStateTempDataProvider requires SessionState to be enabled.
Using Reflector I found that this is due to the fact that HttpContext's Session property == null.  So I guess my question is: is there a better way to do this?  Am I even attacking this the right way?  It's awful hard to queue up all those Context objects...  Should I be doing this a different way?

- Noah






--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Noah Blumenthal ([address removed]) from New York .NET Meetup Group.
To learn more about Noah Blumenthal, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA


Ready for Fall shows? Use Bing to find helpful ratings and reviews on digital tv's. Click here.



--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Darshan Mehta ([address removed]) from New York .NET Meetup Group.
To learn more about Darshan Mehta, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway, New York, NY 10012 USA

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy