问题描述
我使用SignalR,它映射到asp.net应用程序虚拟路径上的〜/ signalr。
SignalR动态创建的应用程序的JavaScript代理中心与虚拟路径启动〜/ signalr /集线器。
I'm using SignalR, which maps to asp.net application on virtual path "~/signalr".SignalR dynamically creates javascript proxy hubs on app start with virtual path "~/signalr/hubs".
于是网址[HTTP://的myapp] / signalr /集线器
是动态的JavaScript内容。
我怎么能这个虚拟路径添加到Asp.Net网站优化捆绑?
So the url "[http://myapp]/signalr/hubs"
is where dynamic javascript content is.How can I add this virtual path to Asp.Net Web Optimization Bundling?
Asp.Net网站优化框架支持VirtuPathProvider的:
ASP.NET捆绑/缩小:包括动态生成javascipt的
Asp.Net Web Optimization Framework starting from 1.1 supports VirtuPathProvider's:ASP.NET bundling/minification: including dynamically generated javascipt
其实我也不知道如何使用这些VPP的。可能有人详细解释或更好举个例子?
Actually I don't understand how to use these VPP's. Could somebody explain in details or better give an example?
推荐答案
动态内容到捆绑工艺集成需要以下几步:
Integration of dynamic content into the bundling process requires the following steps:
-
编写请求/构建所需内容的逻辑。对于SignalR你可以使用这个code片断:
Writing the logic that requests / builds the required content. For SignalR you could use this code snippet:
public static string GetSignalRContent()
{
var resolver = new DefaultHubManager(new DefaultDependencyResolver());
var proxy = new DefaultJavaScriptProxyGenerator(resolver, new NullJavaScriptMinifier());
return proxy.GenerateProxy("/signalr");
}
实施了覆盖现有的虚拟路径提供者和拦截应提供动态内容的所有虚拟路径(只是〜/ signalr /集线器你的情况)。
Implement a virtual path provider that wraps the existing one and intercept all virtual paths that should deliver the dynamic content (just "~/signalr/hubs" in your case).
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;
}
}
填写您的虚拟路径提供:
Register your virtual path provider:
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);
}
有关虚拟路径提供一些样品+捆绑,看到的或
For some samples of virtual path providers + bundling, see Bundling and Minification and Embedded Resources or Bundling Dynamic Generated Controller / Action Content for example.
这篇关于如何添加捆绑包与动态内容asp.net网站优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!