微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

原文地址:https://www.jb51.cc/ajax/159997.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐