MVC Razor 视图:输出文本作为原始文本和 HTML 解析?

MVC Razor View: Output text as both raw and HTML parsed?

我很难弄清楚如何做到这一点...

我实际上是在一个名为 "Body" 的 属性 中将一个巨大的博客 post 保存在一个名为 "Post" 的 class 中。在 body 我会有各种各样的东西,比如

<p> Hello world </p>
<p> Some random paragraph </p>
<codeblock> Here is an example of a basic HTML page
<html>
<body>
<h1> Hello Guys ! </h1>
</body>
</html>
</codeblock>

然后我想要一个代码块,因此我希望 HTML/CSS/Javascript/etc 被解析为 HTML encoded/decoded 页面,所以我真的想要标签和尖括号显示在页面上而不是被解析为它们是什么。

我还有一个名为 HTML 的标签,它以 .没什么特别的,它只是缩进并添加了一些特定的 CSS 。我希望标记之前和之后的标记根据需要呈现 HTML 标记。

目前我正在使用

字面输出 Body 属性 的内容
@Html.Raw(post.Body)

当我将它保存到数据库时没有什么特别的:

@Html.TextAreaFor(model => model.Body)

所以对于那些仍然有这个问题的人,这就是我解决它的方法。

1) 我包含了 prettyprint。请参阅下面的 link

https://google-code-prettify.googlecode.com/svn/trunk/README.html

2) 在编辑一个post时我只添加了下面的代码 //一堆代码

示例: var http = require("http");

var server = http.createServer(function(req, res){
console.log(req.url);
resp.write("<html><body>" + req.url + "</html></body>");
resp.end();
});

server.listen(3000);

</pre>

在我的 Razor 视图中,我有以下代码:

<div class="blog margin-bottom-40" onload="prettyPrint()">
//Bunch of other code up here for my view
    <div class="blogpost">
        @Html.Raw(post.Body)
    </div>
</div>

我的博客是www.techiejs.com

随时查看,如果您需要我的解决方案中的其他文件,请告诉我。目前我的 git 存储库是私有的。