Ehsan Ghanbari

Experience, DotNet, Solutions

How to create portable area in asp.net mvc4

Sometimes you need to separate your areas from MVC project into another class library for making it readable and better management.  you can do that by using features of MvcContribe project. I recommend you to create your area in asp.net MVC and then transfer the content to the class library:

 

Contrib

 

Now add a class library to your solution and transfer the content(Controllers, views, RegisterArea, and web.config) of your area to class library (remove all content of area in MVC but Web.Config):

Contrib

 

Now get the MvcCntribe by Nuget and change the PortableAreaRegistration in your class library like this:

 

public class PortbaleAreaRegistration : PortableAreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Portable";
            }
        }
           
        public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
        {
            RegisterRoutes(context);
            RegisterAreaEmbeddedResources();
        }

        private void RegisterRoutes(AreaRegistrationContext context)
        {

            context.MapRoute(
               AreaName + "Portable",
               "Portable/{controller}/{action}/{id}",
               new { Controller = "Home", action = "Index", id = UrlParameter.Optional },
               new[] { "Portable.Controllers" });
        }
    }

 

Now go to the controllers of your class library and add a controller with a simple action:

 

   public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }
    }

 

Then add the View, the most important thing about portable areas is the should make your view as Embed Resource because you are going to read the from dll:

 

Contrib

 

Now it's time to create a custom view engine to support your portable views (although razor will support them):

 

 public class PortableAreaViewEngine : RazorViewEngine
    {
        public PortableAreaViewEngine()
        {
            AreaViewLocationFormats = new[]
            {
                "~/Views/Home/{0}.cshtml"
            };
        }
    }

        public override ViewEngineResult FindView
                                (ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }

        public override ViewEngineResult FindPartialView
                                  (ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }
    }

 

And finally, you should register the MvcContrib and Custome view engine in Global.asax :

 

   protected void Application_Start()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new PortableAreaViewEngine());
            ViewEngines.Engines.Add(new RazorViewEngine());

            AreaRegistration.RegisterAllAreas();
            PortableAreaRegistration.RegisterEmbeddedViewEngine();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }

​​​​​​​

Have fun!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus