views:

117

answers:

2

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!

A: 

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?

Wyatt Barnett
Hi Wyatt, here's the goal (although it doesn't matter really).I have some partial views that should be rendered to a file and the transferred to another server via ftp. So I need a way to produce HTML, but since I already have my partial views, don't want to duplicate code.I don't see how it FUBAR really. If you think about how MVC should be easily testable and all that, how come it's so hard to reproduce the final result of a controller method - HTML?
Bokka
A: 

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?

mannish
mannish, would that service be able to call the controller action directly then? That's also a viable solution for me. Thanks!
Bokka
Even if this is not what I was looking for, I'll mark this as an answer. I did it this way in the end since I didn't want to lose more time on this task.
Bokka