Search code examples
c#asp.net.netasp.net-mvcrazor

Render Razor View to string?


I need to render my razor view to string. I would like to pass my model:

public class ContactDetails
{
    public string Name { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
}

and use it in my razor view and then render it to the string.

I heard about RazorEngine but I would like to do it without it. Also I don't have an controller context.

Maybe I need fake some controller context?

Is it possible to do it? Do you have any idea how can to solve this problem?


Solution

  • You may want to check out https://github.com/RickStrahl/Westwind.RazorHosting - it shows how to render strings using the razor engine without a controller