如何在Razor视图中从数据库渲染Markdown文本? [英] How to render Markdown Text from database in a Razor view?

查看:123
本文介绍了如何在Razor视图中从数据库渲染Markdown文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在处理自己的自定义Route映射(而不是让ServiceStack自动处理它),仅仅是因为我的所有数据都存储在数据库,页面内容以及所有内部.我有一系列_Layout.cshtml文件,我的Markdown存储为字符串.

所以我想问一个问题,我需要为映射的路由继承哪种类型的服务(常规服务?),还需要返回什么状态在字符串Y中使用布局X和Markdown"? /p>

我已经阅读了ServiceStack Wiki和新示例页面上的示例,但无法找到任何实现此目的的示例(一切似乎都从文件中读取Markdown,而Razor从数据库中读取变量).

如果我不清楚我的问题,请让我知道,我很乐意相应地对其进行修改.

-

编辑

一些澄清:

是的,我要加载常规的Razor页面. 是的,我从文件中下载了常规的Markdown文件.

我现在只是好奇如何将Markdown渲染到Razor方法"RenderBody()"中,该方法位于字符串中(来自我的数据库).

解决方案

如果您只想将 Markdown文本呈现为HTML (不是Markdown Razor,即没有剃须刀功能!),则需要做这两个步骤:

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

现在,您只需要在例如适当的位置将渲染的HTML添加到Razor视图中即可.


但是,如果您的目标是在Razor视图中呈现普通的 Markdown视图(即ServiceStack项目中的.md文件),则需要遵循以下报价:

在Razor页面中包括部分Markdown视图

我们非常喜欢Markdown和Razor,因此ServiceStack中包含 带有Razor功能和语法的Markdown增强版 叫做Markdown Razor,对现有产品应该立即熟悉 剃刀用户.

我们希望Razor + Markdown会越来越受欢迎 我们已经扩展了@ Html.Partial()的组合以嵌入 来自不同View Engine的部分.此功能可让您嵌入任何 就像我们在每个内容丰富的Rockstar中所做的Markdown Page一样 使用标准Razor Partial语法的页面:

@Html.Partial("Content")

这告诉ServiceStack在该位置的页面内部嵌入名为Content的Partial.首先,它将查找名为 Content.cshtml,如果到达,则跟一个名为Content.md的Partial Markdown Razor View Engine.最初,它搜索当前 目录,然后是/Views/Shared中的所有匹配Partials 文件夹.

引用自 http://razor.servicestack.net .

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).

解决方案

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.

这篇关于如何在Razor视图中从数据库渲染Markdown文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆