I want to use MVC's DisplayTemplates to format HTML that is included as part of a JSON ajax response.
The HTML is returned as part of an AjaxJsonResult
class (mine) that is created outside of the controller, and so I don't want to use the simple, controller based, RenderViewToString() method that I would otherwise prefer.
I've found 4 potential ways to do this, but none of them please me. And I'm out of my depth to know which is the best one to use, if any. To my eyes they seem overly complicated (1, 4), overly smelly (2) or come with a heavy overhead (3).
Could anybody suggest which of these might be the most efficient / best to use? I don't have the experience to weigh up the pros / cons of each one.
1. RazorEngine
I had planned to use RazorEngine - when I was researching it, it seemed simple enough:
Razor.Parse("DisplayTemplates/MyModel.cshtml", model)
But that method came up as obsolete. When I looked at the docs, there seemed to be quite a few 'breaking changes'. I am struggling to get my head around how I should be implementing, how I should be caching, when to read from disk, etc.
2. Use RenderViewToString() - Passing Controller in as a Parameter
I don't really want to do this as it seems messy passing that further up the chain into other code.
3. Create a Fake Controller
One option is to create a FakeController (the accepted answer to this question), but this seems a bit overkill. I can't remember where I saw it, but I read somewhere that it was not great for performance.
4. Render Class, with Virtual Path Provider
This is another approach I saw, on BuildStarted. Again, I am out of my depth here, so can't say what the pros and cons are of this approach.