2012-12-16

Asp.net MVC: reuse Areas from external project

,

I created a MVC project that has an Area defined that i would like to use in an other MVC project. Here is the structure.

image

I created a reference between the AlphaArea and MainApp project. When i run the application is see that the area registration of the AlphaArea is executed. But when i try to go to the area i get an error.

image

What this error says, is that the view for the Index of the AlphaController in the Alpha area isn’t found. This makes perfect sense because the Index view isn’t declared in the MainApp project and this is where MVC  tries to find the view. We need to make sure that the view engine can resolve the view.

There are a couple solutions to resolve this. We could copy or create a link in the MainApp to the index view that is defined in the AlphaArea. We would need to copy the whole Area structure. Don’t know about you but I'm not really found of that solution. An other solution is to compile the AlphaArea views in the DLL. This can be done by a Visual Studio extension and a Nuget package.

First you need to install the Razor Generator extension for Visual Studio.

image

This extension will create a .cs file for a view, but in the properties of that view, the Custom Tool needs to be set to “RazorGenerator”.  Otherwise the VS extension will not create a .cs file for that view.

image                                                 image

When we compile the project, the view will be added to the DLL of the AlphaArea project. This isn’t enough. Now we need a way to tell the view engine that the view can be found in the DLL. Luckily, there is a Nuget package that does the trick. You simply need to install the package “RazorGenerator.Mvc” in the AlphaArea project.

image

This package will add a file (RazorGeneratorMvcStart) in the AppStart folder. This file will create a PrecompiledMvcEngine and add it to the ViewEngines. The code will be executed when the applications starts. This is done by using the WebActivator.

using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using RazorGenerator.Mvc;

[
assembly: WebActivator.PostApplicationStartMethod(typeof(AlphaArea.App_Start.RazorGeneratorMvcStart), "Start")]

namespace AlphaArea.App_Start {
public static class RazorGeneratorMvcStart {
public static void Start() {
var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {
UsePhysicalViewsIfNewer =
HttpContext.Current.Request.IsLocal
};

ViewEngines.Engines.Insert(0, engine);

// StartPage lookups are done by WebPages.
VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
}
}
}

When we now run the application and go to the Alpha area, the view is resolved.


image


The same system can also be used on Controllers in external projects. The example for the post can be downloaded here.

9 comments:

  1. Excellent Article. I understand that the Alpha Area view gets compiled and added to Main area. How does the Main App know about the controller information in Alpha Area project?

    ReplyDelete
  2. Originator of a standout amongst the best etailers, Amazon, and the energetic creator of Kindle, Jeff Bezos has invested http://uk-essay.net/essays certain genuine time contemplating thoughts and their dissemination. So how does that play out in his each day business procedure?

    ReplyDelete
  3. Very nice hint, thank you.

    This way makes the AlphaArea view rendered, but during the process the rest of the page layout (the _Layout.cs one) is lost.

    The result is a page rendered with only the portion of the area view code, that is not usable in a real life scenario.
    Any idea on how solve this?

    Thanks,
    Marco

    ReplyDelete
  4. Views in the Shared area use _ViewStart.cshtml in the shared area folder, and not in the original Area. In my case this is undesirable, as each Area has its own layout. To see more info please visit http://essayswriters.org/academic-writing/.

    ReplyDelete
  5. If you have any custom viewengines in your application, you should update the default behavior to use Add instead of Insert.
    ViewEngines.Engines.Add(engine); instead of ViewEngines.Engines.Insert(0, engine);

    ReplyDelete
  6. Home improvement just downright scares some folks. With some tools and proper information, it’s possible to complete pretty much any project.
    retro4jordanshoes.com |

    sildenafiledcure.com |

    storeproductsjp.com |

    synectic-technology.com |

    tadalafilblog2013.com |

    uktimberland-sale.com |

    vardenafilinfoblog.com |

    xanaxanxiety2013.com |

    youredmedsinfo.com |

    zolpidem4sleep.com |

    ReplyDelete
  7. If you want to express your positive emotions or celebrate special achievements or occasions with a meaningful material object, jewelry is the perfect choice. Even seemingly small gestures will be treasured for years to come.
    www.coffe3.org |

    www.mardomak.biz |

    www.pgg-blueberg.com |

    www.hdslrcoalition.com |

    software4airlines.com |

    belarusforjournalists.info |

    schmeckts-dir.net |

    pandoraschmuckshop.com |

    thedetailsmovie.com |

    nyyyhg.com |

    shledexpo.com |

    lincolnmaineusa.com |

    bbflatiron.com |

    literaturesw.com |

    pizzabootcamp.com |

    00k.biz |

    cs9988.com |

    xpressionsbysarah.com |

    exbulk.net |

    bearcrawlingnation.com |

    ReplyDelete
  8. Excellent article. Thanks for your help.

    ReplyDelete
  9. I don't know how to reuse the area.This blog helps me to use an area again.Thank you for sharing your wonderful idea with us.
    professional resume writing service

    ReplyDelete