如何MVC4优化允许局部视图脚本? [英] How does MVC4 optimization allow partial view scripts?

查看:112
本文介绍了如何MVC4优化允许局部视图脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与局部的意见和MVC的一个问题是,如果你的可重复使用的局部视图需要一定的javascript,有没有办法将其纳入并加载它在脚本部分页面的底部。不仅仅是性能问题,这意味着需要像jQuery的东西还没有present,你必须使用任何jQuery的依赖code的时髦延迟执行。

One problem with partial views and MVC, is that if your reusable partial view requires certain javascript, there was no way to include it and have it loaded at the bottom of the page in the scripts section. Beyond just the performance issue, it means that things necessary like jquery are not yet present and you have to use funky deferred execution of any jquery dependent code.

这个问题的解决方案将是,以允许在谐音的部分,使得该部分可以注册它的脚本显示在布局的正确位置。

The solution to this problem would be to allow sections in partials, such that the partial can register it's scripts to appear in the correct location of the Layout.

推测,MVC4的优化/捆绑特征都应该解决这个问题。然而,当我在一个部分呼叫@ Scripts.Render,它包括他们的地方是局部的。它没有做任何一种魔法放置脚本在页面的结尾。

Supposedly, MVC4's Optimization/bundling features are supposed to solve this problem. However, when I call @Scripts.Render in a partial, it includes them wherever the partial is. It doesn't do any kind of magic to place the scripts at the end of the page.

下面看看埃里克·波特的评论:
<一href=\"http://aspnet.uservoice.com/forums/41199-general-asp-net/suggestions/2351628-support-section-render-in-partialviews\">http://aspnet.uservoice.com/forums/41199-general-asp-net/suggestions/2351628-support-section-render-in-partialviews

Here see Erik Porter's comment: http://aspnet.uservoice.com/forums/41199-general-asp-net/suggestions/2351628-support-section-render-in-partialviews

为其他一些地方我见过的人说MVC 4解决了这个问题,但没有例子怎么样。

A few other places I've seen people saying MVC 4 solves this problem, but no examples as to how.

我如何包括部分在其他脚本后身体的端不需要脚本,使用MVC4优化来解决这个问题?

How do I include scripts needed by a partial at the end of the body after other scripts, using MVC4 Optimizations to solve the problem?

推荐答案

有一件事你可以做的是创造一些的HtmlHelper扩展方法如下所示:

One thing you can do is create some HtmlHelper extension methods like the following:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;

public static class ScriptBundleManager
{
    private const string Key = "__ScriptBundleManager__";

    /// <summary>
    /// Call this method from your partials and register your script bundle.
    /// </summary>
    public static void Register(this HtmlHelper htmlHelper, string scriptBundleName)
    {
        //using a HashSet to avoid duplicate scripts.
        HashSet<string> set = htmlHelper.ViewContext.HttpContext.Items[Key] as HashSet<string>;
        if (set == null)
        {
            set = new HashSet<string>();
            htmlHelper.ViewContext.HttpContext.Items[Key] = set;
        }

        if (!set.Contains(scriptBundleName))
            set.Add(scriptBundleName);
    }

    /// <summary>
    /// In the bottom of your HTML document, most likely in the Layout file call this method.
    /// </summary>
    public static IHtmlString RenderScripts(this HtmlHelper htmlHelper)
    {
        HashSet<string> set = htmlHelper.ViewContext.HttpContext.Items[Key] as HashSet<string>;
        if (set != null)
            return Scripts.Render(set.ToArray());
        return MvcHtmlString.Empty;
    }
}

从您的谐音,你会使用这样的:

From your partials you would use it like this:

@{Html.Register("~/bundles/script1.js");}

而在你的布局文件:

And in your Layout file:

   ...
   @Html.RenderScripts()
</body>

由于您的谐音所有的脚本包将被注册的布局文件的末尾之前运行,他们将被安全地渲染。

Since your partials run before the end of the layout file all the script bundles will be registered and they will be safely rendered.

这篇关于如何MVC4优化允许局部视图脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆