asp.net-mvc - 将来自 MVC 程序集中的资源的脚本和 CSS bundle 在一起

标签 asp.net-mvc resources bundle

我一直在 MVC 中使用 Bundles 将所有脚本和 CSS 打包在一起,这很棒,但是...... 有没有办法将共享项目库中的资源中的脚本或CSS包含在 bundle 中,或者有人知道类似 bundle 的东西可以做到这一点吗?

最佳答案

我可能会开始编写一个自定义 bundle 转换类来读取您需要的资源并返回其内容和内容类型:

public class ResourceTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        string result;

        using (Stream stream = Assembly.GetExecutingAssembly()
            .GetManifestResourceStream("YourAssemblyNamespace.YourResourceFolder.YourFile.css"))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                result = reader.ReadToEnd();
            }
        }

        response.ContentType = "text/css";
        response.Content = result;
    }
}

对于生产用途,您可能希望减少 ResourceTransform 类的硬编码,并将所需的资源作为参数或属性发送,但您明白了。

这样您就可以将此 bundle 添加到您的收藏中:

Bundle resources = new Bundle("~/css/resources");
    resources.Transforms.Add(new ResourceTransform());
    resources.Transforms.Add(new CssMinify());

bundles.Add(resources);

关于asp.net-mvc - 将来自 MVC 程序集中的资源的脚本和 CSS bundle 在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959974/

相关文章:

asp.net-mvc - ASP.NET MVC 未完全部署

ruby-on-rails - rails 3 资源 "without"

Tomcat 在高负载期间未使用所有内核

java - 与 JRE bundle 在一起的应用程序使用哪些 cacerts

iphone - 我的 iOS 应用程序名称中有一个 +。因此 bundle 无效。需要帮助解决

WebApi 中的 C# await/async,有什么意义?

asp.net-mvc - 动态设置 app.config 文件连接字符串

javascript - 如何触发 JQuery 对话框以从 MVC 分部 View 的 Controller 关闭?

c++ - C++ 智能指针中带循环的引用计数

ios - 快速地,有没有办法从第二个 Realm 包添加新数据,而不覆盖当前默认 Realm 中的任何现有数据?