1

I want to get the rendered HTML my action in my console app, without making a web request.

I have a solution with (for simplicity sake) two projects in it. One is a Console App. One is an MVC4 project.

I have a reference to my MVC app from my console app.

I want to be able to create a new instance of my MVC controller in my console app and say something like:

var controller = new MyController();
var model = new MyModel();
var result = controller.GetHelloWorld(model);

var html = ?? // result.ToHtml();

I would like html to contain the rendered HTML that was returned from the action.

Is this possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ev.
  • 7,109
  • 14
  • 53
  • 87
  • Assuming `GetHelloWorld` action returns a `ViewResult`, take a look at http://stackoverflow.com/questions/483091/render-a-view-as-a-string/484932#484932 – haim770 Nov 18 '13 at 07:10
  • Thanks for the comment, but unfortunately because I'm calling it from a scheduled task the viewContext.HttpContext is null, so I can't use that option. – Ev. Nov 18 '13 at 22:21

1 Answers1

1

You can use the Razor templating engine to render the HTML result.

This example is from the official project codeplex page:

  string template = "Hello @Model.Name! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });
Koala Bear
  • 26
  • 1
  • Thanks, this is close to what I'm looking for, but I'd prefer not to add that component to my solution just to get this one piece of functionality working. It would be cool if it was part of the normal Razor View engine. – Ev. Nov 18 '13 at 22:24