2012-12-21 17 views
23

से सीएसएस फ़ाइल को बाहर मैं इस तरह के बंडलasp.net MVC के बंडल

bundles.Add(new StyleBundle("~/Content/themes/default/css") 
     .IncludeDirectory("~/Content/themes/Default", "*.css")); 

है, लेकिन मैं इसे से एक सीएसएस फ़ाइल निकालना चाहते हैं। क्या यह प्रत्येक सीएसएस फ़ाइल को बंडल में निर्दिष्ट किए बिना बनाना संभव है?

+1

आप उस निर्देशिका से सीएसएस फ़ाइल को क्यों नहीं ले जाते हैं? –

+1

बेशक मैं ऐसा कर सकता हूं और इस फ़ाइल के लिए एक और बंडल निर्दिष्ट कर सकता हूं, लेकिन सभी फ़ाइलों को एक निर्देशिका में रखना बेहतर होगा और यदि मुझे इसकी आवश्यकता नहीं है, तो मैं इसे केवल बंडल से बाहर कर दूंगा – xurca

+0

क्या आपके पास बहुत कुछ है फाइलों का? यदि आपके पास छोटी संख्या में फाइलें हैं तो आप अलग-अलग प्रत्येक सीएसएस को पंजीकृत कर सकते हैं। –

उत्तर

34

IgnoreList.Ignore का उपयोग करने का प्रयास करें; bundles.IgnoreList.Ignore(...)

+2

एक अच्छा उदाहरण देखने के लिए : https://stack247.wordpress.com/2014/04/18/asp-net-mvc-4-bundles-ignore-list/ – Kildareflare

+0

यह एक खराब जवाब है। यह अनदेखा नहीं करता है कि इसका उपयोग कैसे करें या इसे कैसे पहुंचाया जाए। यह एक छोटे से स्निपेट के साथ कुछ खराब दस्तावेज का सिर्फ एक लिंक है जो यहां कोई भी मूर्खता नहीं बताता है। WordPress साइट के बारे में टिप्पणी यहां सबसे उपयोगी बात है। – Liam

13

एक विस्तार विधि हो सकता है कि आप यहां क्या जरूरत है:

public static class BundleExtentions 
{ 
    public static Bundle IncludeDirectoryWithExclusion(this StyleBundle bundle, string directoryVirtualPath, string searchPattern, params string[] toExclude) 
    { 
     var folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath); 

     foreach (var file in Directory.GetFiles(folderPath, searchPattern)) 
     { 
      if (!String.IsNullOrEmpty(Array.Find(toExclude, s => s.ToLower() == file.ToLower()))) 
      { 
       continue; 
      }  

      bundle.IncludeFile(directoryVirtualPath + "/" + file); 
     } 

     return bundle; 
} 

और फिर उपयोग किया जाना चाहिए:

bundles.Add(new StyleBundle("~/Content/themes/default/css") 
    .IncludeDirectoryWithExclusion("~/Content/themes/Default", "*.css", "file-you-dont-want.css")); 

मैं इस समय एक पीसी पर नहीं कर रहा हूँ तो ऊपर संयुक्त राष्ट्र है परीक्षण किया लेकिन आपको अपने समाधान के लिए एक टेम्पलेट देना चाहिए।

+0

मैंने खोज पैटर्न जोड़ने और यहां कम संस्करण को कम करने के लिए इस जवाब को बढ़ाया है। Http://stackoverflow.com/a/36080620/1671558 –

1

यहां एक और विस्तार विधि है जो मौजूदा IncludeDirectory विधियों को अधिभारित करती है और उपनिर्देशिका खोज करने का समर्थन करती है।

public static class BundleExtensions 
{ 
    public static Bundle IncludeDirectory(
     this Bundle bundle, 
     string directoryVirtualPath, 
     string searchPattern, 
     params string[] filesToExclude) 
    { 
     return IncludeDirectory(bundle, directoryVirtualPath, searchPattern, false, filesToExclude); 
    } 
    public static Bundle IncludeDirectory(
     this Bundle bundle, 
     string directoryVirtualPath, 
     string searchPattern, 
     bool searchSubdirectories, 
     params string[] filesToExclude) 
    { 
     var physicalPath = HttpContext.Current.Server.MapPath(directoryVirtualPath); 
     return bundle.Include(Directory 
      .EnumerateFiles(physicalPath, searchPattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly) 
      .Select(physicalFileName => physicalFileName.Replace(physicalPath, directoryVirtualPath).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)) 
      .Where(virtualFileName => !filesToExclude.Contains(virtualFileName)) 
      .ToArray()); 
    } 
} 
4

मैं जॉन मैल्कम से अच्छा सुझाव सुधार हुआ है (और सतपाल here द्वारा कुछ अपडेट) कुछ कमियों है कि यह था ठीक करने के लिए:

1) यह ".min साथ बंडलों के डिफ़ॉल्ट व्यवहार टूट जाता है । " फ़ाइलों

2) यह खोज पैटर्न की अनुमति नहीं है, लेकिन केवल फाइलों

using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Web; 
using System.Web.Optimization; 


public static class BundleExtentions 
{ 
    public static Bundle IncludeDirectoryWithExclusion(this Bundle bundle, string directoryVirtualPath, string searchPattern, bool includeSubDirectories, params string[] excludePatterns) 
    { 
     string folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath); 

     SearchOption searchOption = includeSubDirectories 
             ? SearchOption.AllDirectories 
             : SearchOption.TopDirectoryOnly; 

     HashSet<string> excludedFiles = GetFilesToExclude(folderPath, searchOption, excludePatterns); 
     IEnumerable<string> resultFiles = Directory.GetFiles(folderPath, searchPattern, searchOption) 
                .Where(file => !excludedFiles.Contains(file) && !file.Contains(".min.")); 

     foreach (string resultFile in resultFiles) 
     { 
      bundle.Include(directoryVirtualPath + resultFile.Replace(folderPath, "") 
        .Replace("\\", "/")); 
     } 

     return bundle; 
    } 

    private static HashSet<string> GetFilesToExclude(string path, SearchOption searchOptions, params string[] excludePatterns) 
    { 
     var result = new HashSet<string>(); 

     foreach (string pattern in excludePatterns) 
     { 
      result.UnionWith(Directory.GetFiles(path, pattern, searchOptions)); 
     } 

     return result; 
    } 
} 

एक उदाहरण उपयोग कि मैं कोणीय के साथ शुरू lib फ़ोल्डर से सभी पुस्तकालयों शामिल करने के लिए है बाहर रखा जाना है, लेकिन सभी को छोड़कर

bundles.Add(new Bundle("~/bundles/scripts") 
       .Include("~/wwwroot/lib/angular/angular.js") // Has to be first 
       .IncludeDirectoryWithExclusion("~/wwwroot/lib", "*.js", true, "*.locale.*.js")); 

यह ठीक से यदि आप दोनों angular.min.js और Angular.js ही व्यवहार और में unminified संस्करण जुड़ जाएगा: भाषा लिपियों (एक अलग बंडल में भाषा बाद के आधार पर क्योंकि केवल एक ही जोड़ दिया जाएगा) डीबग और आर में मौजूदा .min.js का उपयोग कर elease।

+0

धन्यवाद! इस जवाब ने एक मुद्दा हल किया जो मुझे बंडल के साथ था। –