0

Trying to render a view to a string inside a bare-bones class. I referenced MVC. I'm looking at this example: https://gist.github.com/eralston/d68287b6634f963c09c7 The problem is, they are assuming this is within the context of an MVC project. I have to provide the dependencies for FindPartialView. I've fiddled around with a bunch of options, here, and can't even get a compileable instance.

Here's one of my attempts. I even tried creating a controller, but I cant figure out how to set any of the options, and MVC controllers are instantiated by the front controller magically, so I don't know what to actually do to configure the controllerContext and TempData. Specifically, what comes to mind is, where can I set my document root?

var viewDataDictionary = new ViewDataDictionary();
var controllerContext = new ControllerContext()
{

};
viewDataDictionary.Model = model;

using (var sw = new StringWriter())
{
    var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
    var viewContext = new ViewContext(controllerContext, viewResult.View, viewDataDictionary, TempData, sw);

}
Bluebaron
  • 2,289
  • 2
  • 27
  • 37
  • Is there no reason you cant just add a controller via the solution explorer? – JamesS Jan 10 '19 at 13:31
  • It's not an MVC project, though I did try to create a Controller, I still couldn't find out how to configure the controller, but, also, I shouldn't need to create a full new controller. I only need a `ControllerContext`. I'm assuming that is instantiated magically in a normal MVC application. – Bluebaron Jan 10 '19 at 13:33
  • This is just a plain Class Library. I've added one class, and that's the contents. Then I ran `install-package System.Web.Mvc MyProj` – Bluebaron Jan 10 '19 at 13:34
  • Don't use MVC for this when you aren't doing MVC. Just use the Razor templating engine. –  Jan 10 '19 at 14:35
  • @Amy, How do I do that? I found this example: https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/comment-page-1 but it doesn't say how I generate the dependencies for `RazorViewToStringRenderer` in a .Net Core Library. – Bluebaron Jan 10 '19 at 15:07
  • Google ".net core razor template engine" and use the library from Github –  Jan 10 '19 at 15:12

0 Answers0