5

We need to render an ActionResult to a string to add pages to our internal search engine index. We settled on this solution to render to string.

I've run into a problem with the ExecuteResult call used to process the View.

Code Snippet:

var oldController = controllerContext.RouteData.Values["controller"];
controllerContext.RouteData.Values["controller"] = 
                  typeof(TController).Name.Replace("Controller", "");

viewResult.ExecuteResult(controllerContext); // this line breaks

I receive the following error:

"Object reference not set to instance of object" error.

I've confirmed viewResult is not null, so the exception has to be thrown internally in ExecuteResult.

What could we be missing?

2
  • Spark provides rendering view to string out of the box and friction less. :) Commented Mar 17, 2010 at 10:25
  • Yeah, but porting all of our views to Spark isn't really practical. Commented Mar 17, 2010 at 16:41

4 Answers 4

2

I figured it out. The issue with ExecuteResult wasn't working as expected was because I hadn't mocked the ControllerContext properly. I was able to solve the issue by mocking the controllercontext as described in this SO post:

Mocking Asp.net-mvc Controller Context

Hope this post helps someone in the future if they're trying to do something similar.

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't tried it myself but Phil Haack did suggest a way to output a view to a string here with MVC in mind.

Comments

0

MVCContrib has a BlockRenderer class which could be helpful for this scenario.

1 Comment

Yeah, but it seems the BlockRenderer class has been deprecated in the latest version of MVCContrib that's compatible with MVC2 RC2.
0

Not a direct answer to your question, but why would you render the whole view as a string to your search index, rather than either a) pushing the data itself into the index (LINQ to Lucene lets you do this w/ attributes) or b) offloading the work to a separate process that crawls the front end using some set of rights?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.