Question

I want to have an console application that I would use to render the output to a file.

Pseudocode:

ComponentBaseController controller = new ComponentBaseController();
SaveToFile("output.html", controller.Result);

I am not using real code here since I have tried different approaches, but nothing gets me near.

The closest I got using Tip # 25 from Stephen Walther, is this:

ComponentBaseController controller = new ComponentBaseController();

RouteData routeData = new RouteData();
routeData.Values.Add("controller", "ComponentBase");

var fakeContext = new FakeControllerContext(controller, routeData);

var result = controller.Details("klasta7") as PartialViewResult;
result.ExecuteResult(fakeContext);
Console.Write(fakeContext.HttpContext.Response.ToString());

This throws an System.InvalidOperationException that the partial view could not be found. Tried different locations for Views folder, but no luck.

Any ideas? Thanks!

Was it helpful?

Solution

I'm not one to try to recommend something other than what you're asking to do, but I agree with Wyatt, the goal seems a little on the sticky side, and at best is going to create some code smell. If you simply need to save the rendered output to a file, I think it'd be easier to have an app service that gets called when you need to do the save, likely from the controller. In that event, you'd have access to a current HttpContext, Routes, etc.

If it's a separate app altogether (the console app exists on it's own), what about setting up a service from within the web app that has a method the console app could call to retrieve the rendered output?

OTHER TIPS

It is going to be tricky to get stuff to render without hosting the app in something as there is no HttpContext, etc.

Now, using WebRequest to grab pages from a running version of the app and dumping them to disk is a bit more doable.

Finally, this sounds a bit FUBAR. What is the goal of this operation?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top