How to render Markdown Text from database in a Raz

2019-07-02 07:20发布

So I am handling my own custom Route mapping (rather than allowing ServiceStack to automatically handle it), simply because all of my data is stored inside of a database, page content and all. I have a series of _Layout.cshtml files, and my Markdown is stored as a string.

So I suppose I am asking, what type of Service do I need to inherit (regular Service?) for my mapped Route, and what do I need to return to state "use Layout X and Markdown in string Y" ?

I have read through the examples on the ServiceStack Wiki and new example page, and was unable to find any samples for achieving this (everything seemed to be reading Markdown from a file, and Razor reading variables from a Database).

Please let me know if I am not clear in my question, and I will be happy to modify it accordingly.

--

EDIT

Some clarification:

Yes I got the regular Razor pages to load. Yes I got the regular Markdown from files to load.

I am simply now curious how to render Markdown into the Razor method "RenderBody()" which is in a string (from my database).

1条回答
放我归山
2楼-- · 2019-07-02 07:27

If you simply want to render a Markdown text as HTML (not Markdown Razor, ie no razor functionality!) you need to do these two steps:

var renderer = new MarkdownSharp.Markdown();
string html = renderer.Transform("___your_markdown_markup_here___");

Now you only have to add the rendered HTML to your Razor view at the appropriate place for example.


However, if your goal is to render a normal Markdown view (ie .md file in your ServiceStack project) in a Razor view, you need to follow this quote:

Include Partial Markdown views in Razor pages

We loved Markdown and Razor so much that included in ServiceStack is an enhanced version of Markdown with Razor functionality and Syntax called Markdown Razor which should be instantly familiar to existing Razor users.

As we expect Razor + Markdown to be an increasingly popular combination we've extend @Html.Partial() support to also embed Partials from different View Engines. This feature lets you embed any Markdown Page as we've done in each of the content-heavy Rockstar pages using the standard Razor Partial syntax:

@Html.Partial("Content")

Which tells ServiceStack to embed a Partial named Content inside the page at that location. First it will look for a Partial named Content.cshtml followed by a Partial named Content.md if it reaches the Markdown Razor View Engine. Initially it searches the current directory, followed by any matching Partials in the /Views/Shared folder.

Quoted from http://razor.servicestack.net.

查看更多
登录 后发表回答