[英]ASP.NET Bundling/Minification and Embedded Resources
I'm trying to use the technique described in this blog to add embedded dll resources to my bundles. 我正在尝试使用此博客中描述的技术将嵌入式DLL资源添加到我的捆绑包中。
I have created the custom VirtualPathProvider
below. 我在下面创建了自定义
VirtualPathProvider
。
public class EmbeddedVirtualPathProvider : VirtualPathProvider {
private Type _rootType;
public EmbeddedVirtualPathProvider(Type rootType) {
_rootType = rootType;
}
public override bool FileExists(string virtualPath) {
if (IsEmbeddedPath(virtualPath))
return true;
else
return Previous.FileExists(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
if (IsEmbeddedPath(virtualPath)) {
return null;
}
else {
return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
public override VirtualDirectory GetDirectory(string virtualDir) {
return Previous.GetDirectory(virtualDir);
}
public override bool DirectoryExists(string virtualDir) {
return Previous.DirectoryExists(virtualDir);
}
public override VirtualFile GetFile(string virtualPath) {
if (IsEmbeddedPath(virtualPath)) {
string fileNameWithExtension = virtualPath.Substring(virtualPath.LastIndexOf("/") + 1);
string nameSpace = _rootType.Namespace;
string manifestResourceName = String.Format("{0}.{1}", nameSpace, fileNameWithExtension);
var stream = _rootType.Assembly.GetManifestResourceStream(manifestResourceName);
return new EmbeddedVirtualFile(virtualPath, stream);
}
else
return Previous.GetFile(virtualPath);
}
private bool IsEmbeddedPath(string path) {
return path.Contains("~/Embedded");
}
}
public class EmbeddedVirtualFile : VirtualFile {
private Stream _stream;
public EmbeddedVirtualFile(string virtualPath, Stream stream)
: base(virtualPath) {
_stream = stream;
}
public override Stream Open() {
return _stream;
}
}
I then register this and set up the bundles; 然后我注册并设置捆绑包;
public static void RegisterBundles(BundleCollection bundles) {
HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedVirtualPathProvider(typeof(My.Custom.Type)));
bundles.Add(new StyleBundle("~/Embedded/css").Include(
"~/Embedded/files/styles/etc/styles.css")
);
}
I have also implemented a custom EmbeddedResourceHttpHandler
as described in the article which works when requesting the file as a direct http request. 我还实现了一个自定义
EmbeddedResourceHttpHandler
,如本文所述,当请求文件作为直接http请求时,它可以正常工作。
Problem: The embedded files aren't being included in the bundle, they're just ignored . 问题:嵌入的文件未包含在捆绑包中, 它们只是被忽略 。 When debugging the
FileExists
method is called several times but never for ~/Embedded/files/styles/etc/styles.css
调试时,
FileExists
调用 FileExists
方法,但从不调用 ~/Embedded/files/styles/etc/styles.css
What am I missing? 我错过了什么?
Secondary Problem 次要问题
When using the latest version of the Microsoft ASP.NET Web Optimization Framework. 使用最新版本的Microsoft ASP.NET Web Optimization Framework时。 The
VirtualPathProvider
works as expected, however it prevents the IRouteHandler
from executing. VirtualPathProvider
按预期工作,但它阻止IRouteHandler
执行。 If the FileExists
method is changed to return false this allows the RouteHandler to execute but obviously breaks the VirtualPathProvider
. 如果将
FileExists
方法更改为return false,则允许RouteHandler执行,但显然会破坏VirtualPathProvider
。
I'm assuming it's not using a configured route because it's looking for a physical file when FileExists
returns true? 我假设它没有使用已配置的路由,因为当
FileExists
返回true时它正在查找物理文件? Is this a configuration or an implementation issue? 这是配置还是实施问题?
You will need to tell the BundleTable to use your VirtualPathProvider like this: 您需要告诉BundleTable使用您的VirtualPathProvider,如下所示:
BundleTable.VirtualPathProvider = new EmbeddedVirtualPathProvider(typeof(My.Custom.Type));
This functionality was added in v1.1.0 of the Microsoft ASP.NET Web Optimization Framework . 此功能已添加到Microsoft ASP.NET Web Optimization Framework的v1.1.0中 。
Also you need to make sure that requests for CSS file go through the ASP.NET pipeline by adding this to your web.config. 此外,您需要通过将此文件添加到web.config来确保CSS文件的请求通过ASP.NET管道。
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
The secondary problem can be solved by setting RouteCollection.RouteExistingFiles
to true
可以通过将
RouteCollection.RouteExistingFiles
设置为true
来解决次要问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.