简体   繁体   中英

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?

您可能想看看https://github.com/RickStrahl/Westwind.RazorHosting-它显示了如何使用剃须刀引擎在没有控制器的情况下渲染字符串

There's a much simpler library called Razor.Templating.Core which works in both MVC and out of MVC Context. Supports .NET Core 3.1 and above versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM