2013-12-16 4 views
8

Ich habe ein modulares MVC-Projekt erstellt, um andere spezielle Webprojekte innerhalb der gleichen Laufzeit zu laden.MVC: Die unter '~ /.../ Index.cshtml' gefundene Ansicht wurde nicht erstellt

Andere Projekte befinden sich in einem Ordner im Stammverzeichnis der Website mit dem Namen "Module". Ich verwende das Attribut PreApplicationStartMethod, um die anderen Assemblys in den Unterverzeichnissen beim Booten zu laden.

Ich habe spezielle Routen hinzugefügt, um jedes Modul mit den Namespace-Einschränkungen zu targetieren.

Ich habe eine Klasse erstellen, die RazorViewEngine implementiert das viewPath außer Kraft zu setzen, wenn ein Anruf für das Element in dem Modul hergestellt wird: ~/Views/Home/Index.cshtml -> ~/Module/ModuleTest/Ansichten /Home/Index.cshtml.

Die Index() Methode innerhalb der dynamisch geladenen Bibliothek ist erfolgreich aufgerufen, aber ich habe einen Fehler, wenn die Ansicht wiedergegeben wird:

Siehe folgendes Bild: http://i.imgur.com/KoTgxg2.png

Das Framework sagen Sie mir, im Grunde, dass die View wurde gefunden, aber er wird es nicht wiedergeben. Hat jemand eine Idee, warum das Framework es ablehnt, es zu rendern?

Serverfehler in '/' Anwendung.

Die Ansicht unter '~/Modules/ModuleTest/Views/Home/Index.cshtml' wurde nicht erstellt.

Beschreibung: Bei der Ausführung der aktuellen Webanforderung ist eine nicht behandelte Ausnahme aufgetreten. Bitte überprüfen Sie die Stack-Trace für weitere Informationen über den Fehler und wo es aus dem Code stammt.

Ausnahmedetails: System.InvalidOperationException: Die Ansicht unter '~/Modules/ModuleTest/Views/Home/Index.cshtml' wurde nicht erstellt.

  • Exception
 
Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[InvalidOperationException: The view found at '~/Modules/ModuleTest/Views/Home/Index.cshtml' was not created.] 
    System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +362 
    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106 
    System.Web.Mvc.Async.c__DisplayClass28.b__19() +321 
    System.Web.Mvc.Async.c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +185 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 
    System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 
    System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 
    System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408 
  • -Controller

    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(new TestModel() { Value = "Bla" }); 
     } 
    } 
  • Ansicht
 

    @using Easily.ModuleTest.Models; 
    @{ ViewBag.Title = "Test Index"; } 
    @model TestModel 
    @Model.Value 

  • CustomRazorViewEngine

    public class CustomRazorViewEngine : RazorViewEngine 
    { 
     public CustomRazorViewEngine() 
     { 
      List tmpViewLocationFormats = new List(ViewLocationFormats); 
      List tmpMasterLocationFormats = new List(MasterLocationFormats); 
      List tmpPartialViewLocationFormats = new List(PartialViewLocationFormats); 
      foreach (string moduleDirectory in EasilyModulesContainer.Modules.Select(x => x.Directory)) 
      { 
       foreach (string viewLocationFormat in ViewLocationFormats) 
        tmpViewLocationFormats.Add(viewLocationFormat.Replace("~/", string.Format("~/{0}/{1}/", Constants.ModulesDirectory, moduleDirectory))); 
       foreach (string masterLocationFormat in MasterLocationFormats) 
        tmpMasterLocationFormats.Add(masterLocationFormat.Replace("~/", string.Format("~/{0}/{1}/", Constants.ModulesDirectory, moduleDirectory))); 
       foreach (string partialViewLocationFormat in PartialViewLocationFormats) 
        tmpPartialViewLocationFormats.Add(partialViewLocationFormat.Replace("~/", string.Format("~/{0}/{1}/", Constants.ModulesDirectory, moduleDirectory))); 
      } 
      ViewLocationFormats = tmpViewLocationFormats.ToArray(); 
      MasterLocationFormats = tmpMasterLocationFormats.ToArray(); 
      PartialViewLocationFormats = tmpPartialViewLocationFormats.ToArray(); 
     } 

     protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
     { 
      return base.CreateView(controllerContext, GetPath(controllerContext, viewPath), masterPath); 
     } 

     protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) 
     { 
      return base.CreatePartialView(controllerContext, GetPath(controllerContext, partialPath)); 
     } 

     private string GetPath(ControllerContext controllerContext, string path) 
     { 
      if (!controllerContext.RouteData.Values.ContainsKey("_module")) 
       return path; 
      Module module = ModulesContainer.Modules.SingleOrDefault(x => x.Name == controllerContext.RouteData.GetRequiredString("_module")); 
      return path.Replace("~/", string.Format("~/{0}/{1}/", Constants.ModulesDirectory, module.Directory)); 
     } 
    } 
+0

ist Ihre Ansicht frei von Fehlern? –

+0

Was ist der Kompilierungsmodus? Wird es in compilationMode = "Always" umgewandelt? –

+0

Ich habe es versucht, aber es ändert nichts leider – Lex2193

Antwort

8

fand ich das Problem, indem das Debuggen in http://aspnetwebstack.codeplex.com/.

Ich lade tatsächlich die Bibliothek, die die Controller aus einem anderen Ordner als bin in meiner ModulesContainer Klasse enthalten (siehe Frage).Aber im Innern System.Web.Mvc.dll, ein Verfahren versuchen, meinen Controller-Typen zu finden, indem ein Assembly.Load() im Standardverzeichnis zu tun, sein, warum die BuildManager.GetCompiledType() Rückkehr null.

Ich habe eine einfache Art und Weise fanden diese Methode überschrieben mit:

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;

und

private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args) 
    { 
     if (args.RequestingAssembly != null) 
      return args.RequestingAssembly; 
     Module module = _modules.SingleOrDefault(x => x.Assembly.FullName == args.Name); 
     if (module != null) 
      return module.Assembly; 
     throw new Exception(string.Format("Unable to load assembly {0}", args.Name)); 
    }

Ich schaue mich nur in meinem vorgefertigten DLL-Cache finde die bereits geladene Baugruppe.

+2

Das sieht nützlich aus, aber wo ist die Variable _modules definiert und ausgefüllt? – Jeremy

0

@ Lex2193: Ich habe ein Problem in Ihrer Antwort gefunden. Wenn ich Ihren Code benutzte es mit Ausnahme Situation gut funktioniert, wo referenzierte Assembly einen anderen Bezug hat, auf dem es abhängt:

Module [M] => Dependency [D] => DeepDependency [DD] 

Wenn Sie in Modulobjekt verwenden, um von [D], dass einige Anrufe in es muss etwas in [DD]. Es wird auf TypeLoad-Ausnahme fehlschlagen. Wegen anfragender Versammlung. So habe ich Code zum ersten searching für Modul bearbeitet:

public static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args) 
{ 
    Assembly module = modules.SingleOrDefault(x => x.FullName == args.Name); 
    if (module != null) 
     return module; 

    if (args.RequestingAssembly != null) 
     return args.RequestingAssembly; 

    throw new Exception(string.Format("Could not load file or assembly {0}", args.Name)); 
} 
Verwandte Themen