【发布时间】:2011-07-28 06:03:31
【问题描述】:
在我的网站上,我有很多内容页面。其中大多数是纯文本,但有些确实包含链接和类似内容。 我使用 wordpress 作为我存储页面正文的后端 cms,并使用 webrequests 将帖子的内容加载到通用 ContentModel 中:
public class WordPressPostModel
{
public string Title;
public string Content;
public string postId;
public string slug;
public string lang;
public string CategorySlug;
public DateTime CacheDate = new DateTime();
public bool NotFound = true;
public WordPressPostModel()
{
}
}
如果我可以将返回的内容视为(部分)cshtml 视图并渲染 ActionLinks 和其他 Html 帮助程序,那就太好了。有没有办法做到这一点?
解决方案按照 Darin 的建议使用 RazorEngine:
由于 Razor 引擎不支持 html 助手,您必须通过从自定义模板库中调用它们来解决此问题,例如
public abstract class MyCustomTemplateBase<T> : TemplateBase<T>
{
public string ActionLink(string linkText, string actionName, string controllerName)
{
string link = HtmlHelper.GenerateLink(HttpContext.Current.Request.RequestContext, RouteTable.Routes, linkText, "Default", actionName, controllerName, null, null);
return link;
}
}
然后像这样使用它:
Razor.SetTemplateBase(typeof(MyCustomTemplateBase<>));
string raw = HttpUtility.HtmlDecode(Content);
string result = Razor.Parse(raw, this);
【问题讨论】:
标签: razor