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:

  1. public class PortbaleAreaRegistration : PortableAreaRegistration
  2.     {
  3.         public override string AreaName
  4.         {
  5.             get
  6.             {
  7.                 return "Portable";
  8.             }
  9.         }
  10.            
  11.         public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
  12.         {
  13.             RegisterRoutes(context);
  14.             RegisterAreaEmbeddedResources();
  15.         }
  16.  
  17.         private void RegisterRoutes(AreaRegistrationContext context)
  18.         {
  19.  
  20.             context.MapRoute(
  21.                AreaName + "Portable",
  22.                "Portable/{controller}/{action}/{id}",
  23.                new { Controller = "Home", action = "Index", id = UrlParameter.Optional },
  24.                new[] { "Portable.Controllers" });
  25.         }
  26.     }

 

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

 

  1.    public class HomeController : Controller
  2.     {
  3.         public ActionResult Index()
  4.         {
  5.             return View("Index");
  6.         }
  7.     }

 

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 custome view engine to support your portable views (although razor will support them):

 

  1.   public class PortableAreaViewEngine : RazorViewEngine
  2.     {
  3.         public PortableAreaViewEngine()
  4.         {
  5.             AreaViewLocationFormats = new[]
  6.             {
  7.                 "~/Views/Home/{0}.cshtml"
  8.             };
  9.         }
  10.     }
  11.  
  12.         public override ViewEngineResult FindView
  13.                                 (ControllerContext controllerContext, string viewName, string masterName, bool useCache)
  14.         {
  15.             return base.FindView(controllerContext, viewName, masterName, useCache);
  16.         }
  17.  
  18.         public override ViewEngineResult FindPartialView
  19.                                   (ControllerContext controllerContext, string partialViewName, bool useCache)
  20.         {
  21.             return base.FindPartialView(controllerContext, partialViewName, useCache);
  22.         }
  23.     }
  24.  

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

 

  1.    protected void Application_Start()
  2.         {
  3.             ViewEngines.Engines.Clear();
  4.             ViewEngines.Engines.Add(new PortableAreaViewEngine());
  5.             ViewEngines.Engines.Add(new RazorViewEngine());
  6.  
  7.             AreaRegistration.RegisterAllAreas();
  8.             PortableAreaRegistration.RegisterEmbeddedViewEngine();
  9.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  10.             RouteConfig.RegisterRoutes(RouteTable.Routes);
  11.             BundleConfig.RegisterBundles(BundleTable.Bundles);
  12.  
  13.         }
  14.  

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. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus