通用html解析器,razor-从外部存储的.cshtml解析Rarzor HTML帮助器

在我的网站上,我有很多内容页面.它们大多数是纯文本,但有些确实包含链接和类似内容.

我将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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值