加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:37:48 所属栏目:百科 来源:网络整理
导读:我在客户端使用mustache.js,在ASP.NET MVC3项目中使用Nustache. 我在服务器上的View文件夹中有Person.mustache模板,我这样使用: @Html.Partial("Person") 来自Razor主视图(Index.cshtml). 但是我如何将其转移到客户端?浏览器无权访问Views文件夹以获取模板
我在客户端使用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读