我可以在template.cshtml中使用RazorEngine吗?
这里我有一个文本字符串为的资源文件,如下所示: 尊敬的@Model.SupporterName,@Model.ConsumerName,来自公司 @Model.ConsumerCompany开放访问团队的下一个许可证聚集 工作 在my View.cshtml上,我使用资源中的字符串我可以在template.cshtml中使用RazorEngine吗?,razor,resources,razorengine,Razor,Resources,Razorengine,这里我有一个文本字符串为的资源文件,如下所示: 尊敬的@Model.SupporterName,@Model.ConsumerName,来自公司 @Model.ConsumerCompany开放访问团队的下一个许可证聚集 工作 在my View.cshtml上,我使用资源中的字符串 <tr> <td class="free-text"> @Resources.ActivatedBodyText </td> </tr>
<tr>
<td class="free-text">
@Resources.ActivatedBodyText
</td>
</tr>
@Resources.ActivatedBodyText
并使用指定的模型运行编译
但是
全部渲染后,html文件看起来像:
如何使用RazorEngine从资源中呈现字符串???以下内容对我很有用: 结果:
public ActionResult Index()
{
string templateStr = Resources.ActivatedBodyText;
Template product = new Template() { SupporterName = "Support1", ConsumerCompany = "Company1", ConsumerName = "Consumer1" };
string pattern = @"\@Model\.(\w+)";
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(templateStr);
foreach (Match match in matches)
{
string capture = match.Groups[1].Value;
string propertyvalue = product.GetType().GetProperty(capture).GetValue(product).ToString();
templateStr = templateStr.Replace(match.ToString(), propertyvalue);
}
ViewBag.templateStr = templateStr;
return View();
}
控制器操作:
public ActionResult Index()
{
string templateStr = Resources.ActivatedBodyText;
Template product = new Template() { SupporterName = "Support1", ConsumerCompany = "Company1", ConsumerName = "Consumer1" };
string pattern = @"\@Model\.(\w+)";
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(templateStr);
foreach (Match match in matches)
{
string capture = match.Groups[1].Value;
string propertyvalue = product.GetType().GetProperty(capture).GetValue(product).ToString();
templateStr = templateStr.Replace(match.ToString(), propertyvalue);
}
ViewBag.templateStr = templateStr;
return View();
}
说明:
public ActionResult Index()
{
string templateStr = Resources.ActivatedBodyText;
Template product = new Template() { SupporterName = "Support1", ConsumerCompany = "Company1", ConsumerName = "Consumer1" };
string pattern = @"\@Model\.(\w+)";
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(templateStr);
foreach (Match match in matches)
{
string capture = match.Groups[1].Value;
string propertyvalue = product.GetType().GetProperty(capture).GetValue(product).ToString();
templateStr = templateStr.Replace(match.ToString(), propertyvalue);
}
ViewBag.templateStr = templateStr;
return View();
}
我的解决方案对你有用吗?我想这里面有相关的问题