WebOptimizer icon indicating copy to clipboard operation
WebOptimizer copied to clipboard

Using Weboptimizer with Razor Class Libraries

Open tskong opened this issue 3 years ago • 6 comments

Hi,

I've got weboptimizer working nicely with my web app, but when I try to pull in .js or .css files from Razor Class Libraries, the page renders, but I'm getting a 404 on the javascript and css.

Is there some configuration required to get it work with RCL?

Thanks

tskong avatar Sep 16 '22 13:09 tskong

Without a code sample it's hard to advise. But are you putting your js and css files in the wwwroot of your RCL and then referencing them in your project as _content/{name of RCL}/{filename}.css ?

pavloss avatar Oct 12 '22 11:10 pavloss

Hi, Yes exactly that. I can get it to work with the non weboptimized versions of the files, but when I try and get the minified versions sing _content/RazorClassLibrary1/site.min.css, it can't find it.

It's fine in the main web application, it can pick up it's minified files no problem, just the RCL.

tskong avatar Oct 13 '22 07:10 tskong

Not sure if you've found a solution already but I was having a similar issue. I fixed it by adding app.UseWebOptimizer(); to the Startup.cs (in the Configure method, right before app.UseStaticFiles();. I also had to change the bundled URL to something else than /css/myfilename.css because /css is the folder I have my css files in. I changed it to the root, so for example pipeline.AddCssBundle("/_myfilename.css", "css/file1.css", "css/file2.css");

jaoord avatar Feb 16 '23 10:02 jaoord

I'm running into the same problem. WebOptimizer was working before I moved files into the RCL. If I disable minification, then the file works, so I know I have the path right. Responses above suggest that it should work, so I'm not sure what I'm doing wrong.

This is a .NET 6 project.

Example url: https://localhost:7264/_content/RCL.Project/Content/jquery.validate.password.css

DirectoryNotFoundException: Could not find a part of the path 'C:\PathToMainProject\wwwroot\_content\RCL.Project\Content\jquery.validate.password.css'.

Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.OSFileStreamStrategy..ctor(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
Microsoft.Extensions.FileProviders.Physical.PhysicalFileInfo.CreateReadStream()
WebOptimizer.Asset.LoadFileContentAsync(IFileProvider fileProvider, AssetContext config, string sourceFile)
WebOptimizer.Asset.ExecuteAsync(HttpContext context, IWebOptimizerOptions options)
WebOptimizer.AssetBuilder.BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
WebOptimizer.AssetMiddleware.HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Any ideas?

hammond13 avatar Apr 21 '23 14:04 hammond13

Update: my problem appears to be specific to .NET 6. I was able to fix the problem by passing in the environment parameter as suggested here:

https://github.com/ligershark/WebOptimizer/issues/232#issuecomment-1355031387

I still ran into a problem with bundling (https://github.com/ligershark/WebOptimizer/issues/172#issue-899098831). Fortunately for me, that only affected one library file that I was able to get around.

hammond13 avatar Apr 21 '23 15:04 hammond13

I think my PR fixes the problem. #287

gumbarros avatar Oct 09 '23 17:10 gumbarros