How to render Razor in cshtml page with Servicesta

2019-09-11 02:01发布

问题:

I have markdown in string property of my model and would like to render it onto page. If I have html in that same string property I would simply do:

@Html.Raw(Model.BodyHtml)

Is there a same thing if string contains Markdown rather than Html? I.e. something like:

@Html.MarkdownToHtml(Model.BodyMarkdown)

回答1:

The new @Html.RenderMarkdownToHtml() API for this has just been added in v4.0.34+ that's now available on MyGet, which will let you render markdown with:

@Html.RenderMarkdownToHtml(Model.BodyMarkdown)

In earlier versions of ServiceStack you would call the Markdown implementation itself to render HTML and use the AsRaw() extension method so the returned HTML isn't automatically encoded, e.g:

@(new MarkdownSharp.Markdown().Transform(Model.BodyMarkdown).AsRaw())