我正在使用 mustache.js 在客户端和 mustache 在 ASP.NET MVC3 项目中。
我有 Person.mustache
服务器上的 View 文件夹中的模板,我像这样使用:
@Html.Partial("Person")
从 Razor 主 View (
Index.cshtml
)。但是我怎样才能把它转移到客户端呢?浏览器无权访问 Views 文件夹以获取模板的原始内容。不知何故,我必须有一种方法来包含在服务器上输出 Person.mustache 模板的 HTML 原始文本。如果我从 Razor View 中需要它,它会编译它,因为它是普通的服务器模板引擎。
请任何人都可以提供任何想法?谢谢。
最佳答案
嗯,我做了一些东西,行得通,也许有人会想出更好的解决方案。这里是:
@Html 助手的扩展:
public static class ViewExtensions
{
public static IHtmlString RenderRawContent(this HtmlHelper helper, string serverPath)
{
string filePath = HttpContext.Current.Server.MapPath(serverPath);
//load from file
StreamReader streamReader = File.OpenText(filePath);
string markup = streamReader.ReadToEnd();
streamReader.Close();
return new HtmlString(markup);
}
}
并在索引页面的 Razor 主 View 中
@using MyProject.Helpers;
<script type="text/template" id="person_template">
@Html.RenderRawContent("~/Templates/Person.mustache")
</script>
关于ajax - 在服务器和客户端之间共享 mustache/mustache 模板。 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9702130/