在我的网站上,我有很多内容页面.它们大多数是纯文本,但有些确实包含链接和类似内容.
我将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 Helper,那将是很棒的.有没有办法做到这一点?
使用Darin建议的RazorEngine解决方案:
由于Razor引擎不支持html帮助程序,因此您必须通过在自定义模板库内部调用它们来解决此问题.
public abstract class MyCustomTemplateBase : TemplateBase
{
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);