Multiple content subfolders in a MvcContrib PortableArea

372 Views Asked by At

I'm looking to create a common ASP.MVC (c# and razor) base project containing common controllers, _Layout.cshtml, css and js for many of our webapps to extend from.

I thought that using MvcContrib and creating Portable Areas is my best bet

So the folder setup is roughly like this

BaseProj

  • Content
    • js
      • plugins
      • misc
    • images
      • foo
    • css
  • Controllers
  • Views

MainProj

  • Content
  • Controllers
  • Views

I am registering the BaseProj area by extending PortableAreaRegistration class (as per MvcContrib docs) so this url works...

htttp://localhost/MainProj/BaseProj/

Looking in the MvcContrib code for PortableAreaRegistration it also registers 3 static routes too (images, styles, scripts) under Content.

Therefore this url works fine too...

htttp://localhost/MainProj/BaseProj/images/bar.jpg

also subfolders work fine only if i use a dot instead of a slash... htttp://localhost/MainProj/BaseProj/images/foo.bar.jpg

However its really useful to have subfolders under images, css, js etc for organisation purposes. These subfolders will now 404 though

ie this will not work.... htttp://localhost/MainProj/BaseProj/images/foo/bar.jpg

So the question is how do i map subfolders under images (without the dot notation)? ie so this works.... htttp://localhost/MainProj/BaseProj/images/foo/bar.jpg

Thanks

EDIT:

Well a bit of thinking and I found a solution. I wonder if this kind of thing might be included in MvcContrib.Portable Areas by default.

First create a catch all route in your BaseProjRegistration.cs to catch all subfolders of Content

public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
{
    ...

        //static controller with catch all for all subfolders of content
        context.MapRoute(
            "BaseProjContent",
            "BaseProj/Content/{*resourceName}",
           new { controller = "Content", action = "LoadContent", resourcePath =    "Content" }
       );
    ...
}

Then Create a ContentController class in your BaseProj which will extend MvcContrib.PortableAreas.EmbeddedResourceController.

This will convert slashes to dots to load the BaseProj content from the DLL ie /BaseProj/Content/images/foo/bar.jpg => /BaseProj/Content/images.foo.bar.jpg

public class ContentController : MvcContrib.PortableAreas.EmbeddedResourceController
{

    public ActionResult LoadContent(string resourceName, string resourcePath)
    {
        string actualResourceName = resourceName.Replace("/", ".");
        return base.Index(actualResourceName, resourcePath);
    }
}

This worked for me, but any other recommnedations of how to setup a common base project for lots of webapps to extend are welcome

0

There are 0 best solutions below