开发者

ASP.NET MVC VirtualPathProvider not working under IIS 6

By ASP.NET MVC plugin architecture, Plug-in architecture for ASP.NET MVC

I have separated DLL(plugin) which contains the views, css and javascript files in the resources. So my own VirtualPathProvider will load the content out from the DLL if that is for the plugin. It works all fine during development. But It appears not working once I deployed it in IIS. (I mapped 开发者_开发技巧the whidcard in IIS 6 and the views are showing)

I have registered my VirtualPathProvider in global.asax as

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
    HostingEnvironment.RegisterVirtualPathProvider(new MyVirtualPathProvider());
}

For example. http://localhost/Plugin/MyPlugin.dll/Styles.MyStyles.css

This should be loaded from the plugin.dll but IIS returns 404.

I guess the static files are all handled by the IIS and not went through asp.net and my VirtualPathProvider ? Is there way to get around this? Please shed some light.

Thanks in advance.


If this is IIS 6 you will need a wildcard mapping. See this blog post from Phil Haack.


I've found the workaround by adding the staticFileHandler in the web.config httpHandlers element.

<add verb="GET,HEAD,POST" path="*" type="System.Web.StaticFileHandler" validate="true" />


I've had a number of problems getting an external compiled library containing resources and controllers to work in our MVC environment. It's used across multiple projects and different errors have surfaced in different projects so here's all the things I had to do (so far) to ensure static file handling works:

  1. Include StaticFileHandler in web.config, e.g.:

    <add verb="GET,HEAD" path="*.js" name="Static for js" type="System.Web.StaticFileHandler" />

  2. Ensure Static items are ignored in routing:

    routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\ .(css|js|gif|jpg)(/.*)?" });

  3. Register a Virtual Path Provider, e.g.:

        System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedResourceVirtualPathProvider.Vpp(assemblies.ToArray())
        {
            //you can do a specific assembly registration too. If you provide the assemly source path, it can read
            //from the source file so you can change the content while the app is running without needing to rebuild
            //{typeof(SomeAssembly.SomeClass).Assembly, @"..\SomeAssembly"} 
        });
    
  4. Not required for static files but worth mentioning are what was needed to get the views / controllers working, which was adding MVCContrib and registering the embedded view engine:

    PortableAreaRegistration.RegisterEmbeddedViewEngine();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜