Razor 从外部存储的.cshtml解析Rarzor Html帮助程序
在我的网站上,我有很多内容页面。其中大多数是纯文本,但有些确实包含链接和类似内容。 我使用wordpress作为我的后端cms,在这里我存储页面主体,并使用webrequests将帖子内容加载到通用ContentModel中:Razor 从外部存储的.cshtml解析Rarzor Html帮助程序,razor,Razor,在我的网站上,我有很多内容页面。其中大多数是纯文本,但有些确实包含链接和类似内容。 我使用wordpress作为我的后端cms,在这里我存储页面主体,并使用webrequests将帖子内容加载到通用ContentModel中: public class WordPressPostModel { public string Title; public string Content; public string postId; public string slug;
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;
}
}
公共抽象类MyCustomTemplateBase:TemplateBase
{
公共字符串ActionLink(字符串链接文本、字符串actionName、字符串控制器名称)
{
string link=HtmlHelper.GenerateLink(HttpContext.Current.Request.RequestContext,RouteTable.Routes,linkText,“默认”,actionName,controllerName,null,null);
返回链接;
}
}
然后像这样使用它:
Razor.SetTemplateBase(typeof(MyCustomTemplateBase<>));
string raw = HttpUtility.HtmlDecode(Content);
string result = Razor.Parse(raw, this);
Razor.SetTemplateBase(typeof(MyCustomTemplateBase));
string raw=HttpUtility.HtmlDecode(内容);
string result=Razor.Parse(raw,this);
您可以看看