如何将包含动态内容的包添加到asp.net Web优化中

时间:2013-10-29 05:40:31

标签: asp.net-mvc .net-4.5 bundling-and-minification asp.net-optimization

我正在使用SignalR,它在虚拟路径“〜/ signalr”上映射到asp.net应用程序。 SignalR在应用程序启动时动态创建javascript代理集线器,虚拟路径为“〜/ signalr / hubs”。

因此,网址"[http://myapp]/signalr/hubs"是动态javascript内容的位置。 如何将此虚拟路径添加到Asp.Net Web Optimization Bundling?

从1.1开始的Asp.Net Web优化框架支持VirtuPathProvider: ASP.NET bundling/minification: including dynamically generated Javascript

其实我不明白如何使用这些VPP。有人可以详细解释或更好地举例说明吗?

2 个答案:

答案 0 :(得分:3)

将动态内容集成到捆绑过程中需要执行以下步骤:

  1. 编写请求/构建所需内容的逻辑。对于SignalR,您可以使用此代码段:

    public static string GetSignalRContent()
    {
        var resolver = new DefaultHubManager(new DefaultDependencyResolver());
        var proxy = new DefaultJavaScriptProxyGenerator(resolver, new NullJavaScriptMinifier());
        return proxy.GenerateProxy("/signalr");
    }
    
  2. 实现一个虚拟路径提供程序,它包装现有的虚拟路径提供程序并拦截应该提供动态内容的所有虚拟路径(在您的情况下只是“〜/ signalr / hubs”)。

    public class CustomVirtualPathProvider : VirtualPathProvider
    {
        public CustomActionVirtualPathProvider(VirtualPathProvider virtualPathProvider)
        {
            // Wrap an existing virtual path provider
            VirtualPathProvider = virtualPathProvider;
        }
    
        protected VirtualPathProvider VirtualPathProvider { get; set; }
    
        public override string CombineVirtualPaths(string basePath, string relativePath)
        {
            return VirtualPathProvider.CombineVirtualPaths(basePath, relativePath);
        }
    
        public override bool DirectoryExists(string virtualDir)
        {
            return VirtualPathProvider.DirectoryExists(virtualDir);
        }
    
        public override bool FileExists(string virtualPath)
        {
            if (virtualPath == "~/signalr/hubs")
            {
                return true;
            }
    
            return VirtualPathProvider.FileExists(virtualPath);
        }
    
        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            // BaseClass can't create a CacheDependency for your content, remove it
            // You could also add your own CacheDependency and aggregate it with the base dependency
            List<string> virtualPathDependenciesCopy = virtualPathDependencies.Cast<string>().ToList();
            virtualPathDependenciesCopy.Remove("~/signalr/hubs");
    
            return VirtualPathProvider.GetCacheDependency(virtualPath, virtualPathDependenciesCopy, utcStart);
        }
    
        public override string GetCacheKey(string virtualPath)
        {
            return VirtualPathProvider.GetCacheKey(virtualPath);
        }
    
        public override VirtualDirectory GetDirectory(string virtualDir)
        {
            return VirtualPathProvider.GetDirectory(virtualDir);
        }
    
        public override VirtualFile GetFile(string virtualPath)
        {
            if (virtualPath == "~/signalr/hubs")
            {
                return new CustomVirtualFile(virtualPath,
                    new MemoryStream(Encoding.Default.GetBytes(GetSignalRContent())));
            }
    
            return VirtualPathProvider.GetFile(virtualPath);
        }
    
        public override string GetFileHash(string virtualPath, IEnumerable virtualPathDependencies)
        {
            return VirtualPathProvider.GetFileHash(virtualPath, virtualPathDependencies);
        }
    
        public override object InitializeLifetimeService()
        {
            return VirtualPathProvider.InitializeLifetimeService();
        }
    }
    
    public class CustomVirtualFile : VirtualFile
    {
        public CustomVirtualFile (string virtualPath, Stream stream)
            : base(virtualPath)
        {
            Stream = stream;
        }
    
        public Stream Stream { get; private set; }
    
        public override Stream Open()
        {
             return Stream;
        }
    }
    
  3. 注册虚拟路径提供商:

    public static void RegisterBundles(BundleCollection bundles)
    {
        // Set the virtual path provider
        BundleTable.VirtualPathProvider =
            new CustomVirtualPathProvider(BundleTable.VirtualPathProvider);
    
        Bundle include = new Bundle("~/bundle")
            .Include("~/Content/static.js")
            .Include("~/signalr/hubs");
    
        bundles.Add(include);
    }
    
  4. 对于虚拟路径提供商+捆绑的一些示例,请参阅Bundling and Minification and Embedded ResourcesBundling Dynamic Generated Controller / Action Content

答案 1 :(得分:0)

我不确定是否有办法做到这一点,但另一种方法是在构建时生成/ signalr / hubs javascript。这样你就可以捆绑生成的js文件。

请参阅&#34;如何为SignalR生成的代理创建物理文件&#34; http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client中的部分。