的動態內容到捆紮工藝集成需要下列步驟:
寫請求/建立所要求的內容的邏輯。對於SignalR你可以使用此代碼段:
public static string GetSignalRContent()
{
var resolver = new DefaultHubManager(new DefaultDependencyResolver());
var proxy = new DefaultJavaScriptProxyGenerator(resolver, new NullJavaScriptMinifier());
return proxy.GenerateProxy("/signalr");
}
實現一個包裝現有的虛擬路徑提供者和攔截應提供動態內容的所有虛擬路徑(只是在你的情況下,「〜/ signalr /集線器」 )。
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;
}
}
註冊虛擬路徑提供:
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);
}
對於虛擬路徑提供的一些樣品+捆綁,例如參見Bundling and Minification and Embedded Resources或Bundling Dynamic Generated Controller/Action Content。
這是否與生成動態的CSS?我無法找到Style/Css縮小器的相應對象。 – DonDon