Skip to main content

Consuming built MVC project under other MVC app

The idea is to reuse build application on multiple child application as a lot of Views and Codes need to be same on all children application. This approach can be a good candidate for a multitenant application, some sort of pluggable architecture or reusing MVC app as parent project.

There are three things need to be done to achieve.
  1. Project built event to copy Views from the parent application.
  2. Area registration on child application.
  3. Custom View path through customized Razor view engine.
Project built event to copy Views from the parent application.

This has to be configured on child application to copy from application build event. It can be done by going into project property => Build Events => Edit Post Built (Post-built event command line).
xcopy "$(SolutionDir)"..\“App.Common\App.Common.Web\Views” “$(ProjectDir)\CommonAppLoc\Views\” /s /i /y /f

xcopy "$(SolutionDir)"..\“App.common\App.Common\App.Common.Web\Areas” “$(ProjectDir)\CommonAppLoc\Areas\” /s /i /y /f

xcopy "$(SolutionDir)"..\“\App.Common\App.Common.Web\Content” “$(ProjectDir)\CommonAppLoc\Content\” /s /i /y /f

There are several macros which can help out to get the desired path. In this one, $(SolutionDir) is used to get solution path and then put the relative path for the application. It can be customized based on a need basis.

In above example, I am trying to copy View, Areas and Content to child application to get physical files.

Area registration on child application.

To consume some other controller, Area registration needs to be done in a different manner with namespace parameter to refer codes of the parent controller from somewhere else.

 public override void RegisterArea(AreaRegistrationContext context)  
     name: "User_default",  
     url: "User/{controller}/{action}/{id}",  
     defaults: new { action = "Index", id = UrlParameter.Optional },  
     namespaces: new[] { "App.Common.Web.Areas.User,Controllers" }  

NOTE: If want to have the same path as parent application then Area registration on parent application can be and consumed on child application to have paths

Custom View path through customized Razor view engine.

Since the other app Views are copied to the local project, we need to set up our View Engine to look into the newly copied paths. To do that the default view engine needs to override to have those paths.

If we look into the source code of MVC then can have a better idea

So, we can inherit the same RazorViewEngine and customize the paths based on desired locations.

 public class AppCommonRazor  
 : RazorViewEngine  
   /// <summary>  
   /// Initializes a new instance of the <see cref="AppCommonRazor"/> class.  
   /// </summary>  
   public AppCommonRazor()  
     ViewLocationFormats = ViewLocationFormats.Concat(new string[]  
     PartialViewLocationFormats = PartialViewLocationFormats.Concat(new string[]  
     AreaViewLocationFormats = AreaViewLocationFormats.Concat(new string[]  
     AreaPartialViewLocationFormats = AreaPartialViewLocationFormats.Concat(new string[]  

The above code is just registering CommonApp path for the application to get the views. The CustomViewLoc path is used to add precedence for views. If any view need further customization then it can be just placed under CustomViewLoc, if it is not present then it picks from original view.

Finally, the View engine registration
This above view engine needs to be registered under Global.asax

 ViewEngines.Engines.Add(new MasterAppCommonRazor());  

Popular posts from this blog

Architecture solution composting Repository Pattern, Unit Of Work, Dependency Injection, Factory Pattern and others

Project architecture is like garden, we plant the things in certain order and eventually they grow in similar manner. If things are planted well then they will all look(work) great and easier to manage. If they grow as cumbersome it would difficult to maintain and with time more problems would be happening in maintenance.

There is no any fixed or known approach to decide project architecture and specially with Agile Methodology. In Agile Methodology, we cannot predict how our end products will look like similarly we cannot say a certain architecture will fit well for entire development lifespan for project. So, the best thing is to modify the architecture as per our application growth. I understand that it sounds good but will be far more problematic with actual development. If it is left as it is then more problems will arise with time. Just think about moving plant vs a full grown tree.

Coming to technical side, In this article, I will be explaining about the various techniques tha…

LDAP with ASP.Net Identity Core in MVC with project.json

Lightweight Directory Access Protocol (LDAP), the name itself explain it. An application protocol used over an IP network to access the distributed directory information service.

The first and foremost thing is to add references for consuming LDAP. This has to be done by adding reference from Global Assembly Cache (GAC) into project.json

"frameworks": { "net461": { "frameworkAssemblies": { "System.DirectoryServices": "", "System.DirectoryServices.AccountManagement": "" } } },
These System.DirectoryServices and System.DirectoryServices.AccountManagement references are used to consume LDAP functionality.

It is always better to have an abstraction for irrelevant items in consuming part. For an example, the application does not need to know about PrincipalContext or any other dependent items from those two references to make it extensible. So, we can begin with some bas…

Configuring Ninject, Asp.Net Identity UserManager, DataProtectorTokenProvider with Owin

It can be bit tricky to configure both Ninject and Asp.Net Identity UserManager if some value is expected from DI to configure UserManager. We will look into configuring both and also use OwinContext to get UserManager.

As usual, all configuration need to be done on Startup.cs. It is just a convention but can be used with different name, the important thing is to decorate class with following attribute to make it Owin start-up:

[assembly: OwinStartup(typeof(MyProject.Web.Startup))]
Ninject configuration

Configuring Ninject kernel through method which would be used to register under Owin.

public IKernel CreateKernel() { var kernel = new StandardKernel(); try { //kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); // TODO: Put any other injection which are required. return kernel; } catch { kernel.Dispose(); throw; }…