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

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

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

@Html.Partial("Person")

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

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

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

好吧,我已经做了一些有用的东西,也许有人会想出更好的解决方案.这里是:

@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主页中查看索引页面

@using MyProject.Helpers;

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

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

</script>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...