jgauffin / griffin.mvccontrib

A contribution project for ASP.NET MVC3
http://blog.gauffin.org/tag/griffin-mvccontrib/
GNU Lesser General Public License v3.0
83 stars 40 forks source link

Using EmbeddedFileProvider for plugin css/js files #24

Closed khsliao closed 11 years ago

khsliao commented 12 years ago

Using example project (PluginSystemDemo) for testing, added to PluginService private readonly EmbeddedFileProvider _embeddedContentProvider = new EmbeddedFileProvider(VirtualPathUtility.ToAbsolute("~/")); and in constructor, I added this provider to GriffinVirualPathProvider before the EmbeddedViewFileProvider named _embededProvider.

And in Startup method, added call _embeddedContentProvider.Add(new NamespaceMapping(assembly, Path.GetFileNameWithoutExtension(assembly.Location)));

I added a Content folder to the Plugin.Messaging project Areas folder, created a css file, configured it to be an embedded resource and link to ~/Areas/Messaging/Content/StyleSheet1.css.

If I run the project and check the CSS file, I see all the inherits in the file, as if it’s processed as a view by the ExternalViewFixer, rendering it unusable by the browser. Same happens to JS files.

However, if i don't add the _diskFileProvider to the GriffinVirualPathProvider in PluginService for runtime edits, the css and js files are rendered normally to the browser.

jvdwijngaard commented 12 years ago

The problem is not the EmbeddedFileProvider. It's the ViewFileProvider, which doesn't handle view files different than js or css files, causing each file to be reparsed by the CorrectView function.

The ViewFileProvider is to be able to make changes and see updates in your browser without having to rebuild and is only added to the GriffinVirtualPathProvider when the VisualStudioHelper.IsInVisualStudio flag is true.

My solution for this was to add an AllowedFixFileExtensions array to ViewFileProvider.cs and ONLY reparse "cshtml", "ascx" or "aspx" files, any other file (js/css) will not be reparsed by CorrectView().

By making this change, the filename ViewFileProvider should be changed since it handles content files as well and in a different way. It was a quick fix for me, but maybe a seperate FileProvider.cs should be created to handle files other than views different, just like the EmbeddedFileProvider and the EmbeddedViewFileProvider do. That way you only need to add an extra provider to the GriffinVirtualPathProvider in the if (VisualStudioHelper.IsInVisualStudio) code block.

jgauffin commented 11 years ago

Fixed in the next commit

jgauffin commented 11 years ago

Fixed in the newest nuget packege. Do note that you also have to configure IIS to get it working.

See here: http://www.paraesthesia.com/archive/2011/07/21/running-static-files-through-virtualpathprovider-in-iis7.aspx

RongieZeng commented 11 years ago

Hi, still don't know how to put the static files in Plugin class library and refresh without rebulding, Is it now support by Griffin.MvcContrib. If yes, please tell me how to use it, thanks !

jgauffin commented 11 years ago

what have you tried?

RongieZeng commented 11 years ago

Now i know what's the problem. I found the Griffin.MvcContrib library have "EmbeddedFileProvider", "EmbeddedViewFileProvider" and ViewFileProvider, but no "FileProvider"(or StaticFileProvider).

So i add a class called "StaticFileProvider", which provide the static files(js,css,image, etc) while development. And used it like this:

    private readonly PluginFileLocator _viewfileLocator = new PluginFileLocator();
    private readonly PluginFileLocator _staticFileLocator = new PluginFileLocator();
    private readonly ViewFileProvider _viewFileProvider;
    private readonly StaticFileProvider _staticFileProvider;

    public PluginService()
    {
        _viewFileProvider = new ViewFileProvider(_viewfileLocator, new ExternalViewFixer());
        _staticFileProvider = new StaticFileProvider(_staticFileLocator);

        if (VisualStudioHelper.IsInVisualStudio)
        {
            GriffinVirtualPathProvider.Current.Add(_viewFileProvider);
            GriffinVirtualPathProvider.Current.Add(_staticFileProvider);
        }            
    }