Blog

Filter posts by Category Or Tag of the Blog section!

How to create portable area in asp.net mvc4

Thursday, 13 February 2014

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!

Category: Software

Tags: Asp.Net

comments powered by Disqus