ASP.NET捆绑/缩小和嵌入式资源 [英] ASP.NET Bundling/Minification and Embedded Resources

查看:236
本文介绍了ASP.NET捆绑/缩小和嵌入式资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用<一个描述的技术href=\"http://weblogs.asp.net/imranbaloch/archive/2012/12/29/asp-net-bundling-and-minification-and-embedded-resources.aspx\">this博客嵌入式DLL资源添加到我的包。

I'm trying to use the technique described in this blog to add embedded dll resources to my bundles.

我已经创建的VirtualPathProvider 下面的习俗。

I have created the custom VirtualPathProvider below.

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

我也实现了自定义 EmbeddedResourceHttpHandler 作为请求文件作为一个直接的HTTP请求时,它的工作原理在文章中描述的。

I have also implemented a custom EmbeddedResourceHttpHandler as described in the article which works when requesting the file as a direct http request.

问题:
没有被包含在捆绑中嵌入的文件,的他们只是忽略的。当调试 FILEEXISTS 方法被调用几次,但从来没有为〜/嵌入式/文件/风格的/ etc /样式。 CSS

我是什么失踪?

第二个问题

在使用最新版本的Microsoft ASP.NET网站优化框架。在的VirtualPathProvider 正常工作,但它prevents的 IRouteHandler 的执行。如果 FILEEXISTS 方法更改为返回false这使得RouteHandler执行,但显然打破了的VirtualPathProvider

When using the latest version of the Microsoft ASP.NET Web Optimization Framework. The VirtualPathProvider works as expected, however it prevents the IRouteHandler from executing. If the FileExists method is changed to return false this allows the RouteHandler to execute but obviously breaks the VirtualPathProvider.

我猜想它不使用配置的路线,因为它寻找一个物理文件时, FILEEXISTS 返回true?这是一个配置或一个执行问题?

I'm assuming it's not using a configured route because it's looking for a physical file when FileExists returns true? Is this a configuration or an implementation issue?

推荐答案

您将需要告诉BundleTable使用您的VirtualPathProvider是这样的:

You will need to tell the BundleTable to use your VirtualPathProvider like this:

BundleTable.VirtualPathProvider = new EmbeddedVirtualPathProvider(typeof(My.Custom.Type));

这个功能是在微软的ASP.NET Web优化框架的 V1.1.0增加。

This functionality was added in v1.1.0 of the Microsoft ASP.NET Web Optimization Framework.

此外,你需要确保对CSS文件请求加给你的web.config文件通过ASP.NET管道。

Also you need to make sure that requests for CSS file go through the ASP.NET pipeline by adding this to your web.config.

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">

的第二个问题可以通过设置 RouteCollection.RouteExistingFiles 来解决,以真正

这篇关于ASP.NET捆绑/缩小和嵌入式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆