4

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

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

Asp.Net Web 优化框架从 1.1 开始支持 VirtuPathProvider 的: ASP.NET 捆绑/缩小:包括动态生成的 Javascript

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

4

2 回答 2

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);
    }
    

有关虚拟路径提供程序 + 捆绑的一些示例,请参阅捆绑和缩小以及嵌入式资源捆绑动态生成的控制器/动作内容

于 2014-12-16T15:20:00.390 回答
0

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

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

于 2014-04-24T09:37:49.003 回答