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.
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.
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.
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.
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.
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.
The same system can also be used on Controllers in external projects. The example for the post can be downloaded here.
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?
ReplyDeleteOriginator 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?
ReplyDeleteVery nice hint, thank you.
ReplyDeleteThis 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
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/.
ReplyDeleteIf you have any custom viewengines in your application, you should update the default behavior to use Add instead of Insert.
ReplyDeleteViewEngines.Engines.Add(engine); instead of ViewEngines.Engines.Insert(0, engine);
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.
ReplyDeletewww.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 |
Excellent article. Thanks for your help.
ReplyDeleteI 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.
ReplyDeleteprofessional resume writing service
It is very good blog and useful for students
ReplyDelete.Net Online Course Bangalore
Mostly on other information sites. Are there other sources of information such as this? Many of us have problems and after reading many profiles, I found this profile keyboard tests.
ReplyDeleteworld777 bet
ReplyDeleteultra luxury flat in kolkata
best coaching classes for class 11 in gurgaon
kurti plazo set under 500
kurti for women
azure firewall
azure blueprints
azure resource group
azure application gateway
azure express route
perde modelleri
ReplyDeleteMOBİL ONAY
Mobil odeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
smm panel
ReplyDeleteSMM PANEL
iş ilanları
instagram takipçi satın al
Hırdavatçı
beyazesyateknikservisi.com.tr
SERVİS
tiktok jeton hile
Good content. You write beautiful things.
ReplyDeletemrbahis
hacklink
mrbahis
sportsbet
hacklink
sportsbet
korsan taksi
vbet
vbet
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletebaşakşehir
ReplyDeletebayrampaşa
beşiktaş
beykoz
beylikdüzü
87GE