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

后端 未结 7 1816
陌清茗
陌清茗 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:24

    You need to embed your static assets into your Razor Class Library assembly. I think the best way to get how to do it is to take a look at ASP.NET Identity UI source codes.

    You should take the following 4 steps to embed your assets and serve them.

    1. Edit the csproj file of your Razor Class Library and add the following lines.

       
        ....
             true
        ....
       
      
       
           ....
           
           
           
           
          .....
       
      
      
          
          
      
      
    2. In your Razor Class Library, create the following class to serve and route the assets. (it assumes your assets are located at wwwroot folder)

      public class UIConfigureOptions : IPostConfigureOptions
      {
          public UIConfigureOptions(IHostingEnvironment environment)
          {
              Environment = environment;
          }
          public IHostingEnvironment Environment { get; }
      
          public void PostConfigure(string name, StaticFileOptions options)
          {
              name = name ?? throw new ArgumentNullException(nameof(name));
              options = options ?? throw new ArgumentNullException(nameof(options));
      
              // Basic initialization in case the options weren't initialized by any other component
              options.ContentTypeProvider = options.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
              if (options.FileProvider == null && Environment.WebRootFileProvider == null)
              {
                  throw new InvalidOperationException("Missing FileProvider.");
              }
      
              options.FileProvider = options.FileProvider ?? Environment.WebRootFileProvider;
      
              var basePath = "wwwroot";
      
              var filesProvider = new ManifestEmbeddedFileProvider(GetType().Assembly, basePath);
              options.FileProvider = new CompositeFileProvider(options.FileProvider, filesProvider);
          }
      }
      
    3. Make the dependent web application to use your Razor Class Library router. In the ConfigureServices method of Startup Class, add the following line.

      services.ConfigureOptions(typeof(UIConfigureOptions));
      
    4. So, now you can add a reference to your file. ( let's assume it's located at wwwroot/js/app.bundle.js).

      
      

提交回复
热议问题