在服务器和客户端之间共享mustache/nustache模板.ASP.NET MVC

Rom*_*man 8 ajax asp.net-mvc templates razor mustache

我在客户端使用mustache.js,在ASP.NET MVC3项目中使用Nustache.

Person.mustache在服务器上的View文件夹中有模板,我使用如下:

@Html.Partial("Person")
Run Code Online (Sandbox Code Playgroud)

来自Razor主视图(Index.cshtml).

但是如何将其转移到客户端?浏览器无权访问Views文件夹以获取模板的原始内容.不知何故,我必须有一种方法来包含在服务器上输出Person.mustache模板的HTML原始文本.如果我从Razor视图中需要它,它会编译它,因为它是普通的服务器模板引擎.

请有人能提出任何想法吗?谢谢.

Rom*_*man 3

好吧,我已经做了一些有效的事情,也许有人会想出更好的解决方案。这里是:

@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);

    }
}
Run Code Online (Sandbox Code Playgroud)

在 Razor 主视图中的索引页面

@using MyProject.Helpers;

<script type="text/template" id="person_template">

    @Html.RenderRawContent("~/Templates/Person.mustache")

</script>
Run Code Online (Sandbox Code Playgroud)