Can Razor Class Library pack static files (js, css etc) too?

后端 未结 7 1815
陌清茗
陌清茗 2020-11-30 01:50

Maybe duplicate of this already, but since that post does not have any answer, I am posting this question.

The new Razor Class Library is awesome, but it cannot pack

7条回答
  •  心在旅途
    2020-11-30 02:34

    I reached this issue long time ago. You may follow the article how to include static files in a razor library that explains how to get around this issue.

    The solution I reached after a lot of investigation is not far from the accepted answer in this question:

    1 Set the files as embedded resources

      
        
        
      
    

    2 Include static files in the manifest

      
        netcoreapp2.1
        true
      
    

    3 Find the relevant folders

    (you can find more details in the linked article, even automatize this search from this article )

    // first we get the assembly in which we want to embedded the files
    var assembly = typeof(TypeInFeatureAssembly).Assembly;
    
    // we filter only files including a wwwroot name part
    filePaths = (from rn in assembly.GetManifestResourceNames().Where(rnn => rnn.Contains(".wwwroot."))
        let hasArea = rn.Contains(".Areas.")
        let root = rn.Substring(0, rn.LastIndexOf(".wwwroot.") + ".wwwroot.".Length)
        let rootPath = !hasArea ? root : root.Substring(0, root.IndexOf(".Areas."))
        let rootSubPath = !hasArea ? "" : root.Substring(root.IndexOf(".Areas.")).Replace('.', '/')
        select  hasArea ? rootSubPath.Substring(1, rootSubPath.Length - 2) : "wwwroot" )
        .Distinct().ToList();
    

    This extracts all embedded resources in a wwwroot folder located in a relevant path.

    4 Add the found paths to the webroot file provider

    var allProviders = new List();
    allProviders.Add(env.WebRootFileProvider);
    allProviders.AddRange(filePaths.Select(t => new ManifestEmbeddedFileProvider(t.Assembly, t.Path)));
    env.WebRootFileProvider = new CompositeFileProvider(allProviders);
    
    

提交回复
热议问题