0

Während ich ein Plugin für eine MVC-Site erstellen, stehe ich ein Problem mit der Controller-Auflösung.@ Html.Action und Controller mit dem gleichen Namen

Hier sind alle Routen, die den aktuellen Kontext betreffen:

routes.MapRoute("Route1", 
    "Admin/Product/{action}/{id}", 
    new { controller = "Product", action = "Index", area = "Admin", id = UrlParameter.Optional }, 
    new[] { "Plugin.Controllers" } 
) 

routes.MapRoute(
    "Admin_default", 
    "Admin/{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", area = "Admin", id = "" }, 
    new[] { "Admin.Controllers" } 
); 

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
    new[] { "Public.Controllers" } 
); 

Es gibt drei Klassen Product in der gesamten Lösung:

  • Plugin.Controllers.ProductController (erbt von der Admin-one)
  • Admin.Controllers.ProductController
  • Public.Controllers.ProductController

In meiner Ansicht "/ Views/Home/Index.cshtml" (d. H. die „Index“ Aktion des Homecontroller ohne Fläche) Ich habe den folgenden Code:

@Html.Action("HomepageProducts", "Product") 

Es bezieht sich auf die Aktion „Startseite“ des Public.Controllers .ProductController.

MVC sieht für den Plugin-Controller statt der Öffentlichkeit ein und wirft die folgende Ausnahme:

[Httpexception (0x80004005): eine öffentliche Aktion Methode ‚Startseite‘ wurde nicht auf Controller 'gefunden Plugin.Controllers .ProductController‘.] System.Web.Mvc.Controller.HandleUnknownAction (String action) +291 System.Web.Mvc.Controller.b__1d (IAsyncResult asyncResult, ExecuteCoreState Innerstate) +31 System.Web.Mvc.Async. WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49 System.Web.Mvc.Cont roller.EndExecuteCore (IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.b__15 (IAsyncResult asyncResult, Controller Controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49 System.Web .Mvc.Controller.EndExecute (IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute (IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.b__5 (IAsyncResult asyncResult, ProcessRequestState Innerstate) +21 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49 System.Web.Mvc.MvcHandler.EndProcessRequest (IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler. System.Web.IHttpAsyncHandler.EndProcessRequest (IAsyncResult-Ergebnis) + 9 System.Web.Mvc. <> c__DisplayClassa.b__9() +22 System.Web.Mvc. <> c__DisplayClass4.b__3() +10 System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap (Func`1 func) +27

[Httpexception (0x80004005): Die Ausführung des Kindes Anfrage fehlgeschlagen. Weitere Informationen finden Sie in der InnerException.] System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap (Func`1 func) +102 System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap (Aktionsaktion) +64 System.Web.Mvc. ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest (IAsyncResult-Ergebnis) +71 System.Web.HttpServerUtility.ExecuteInternal (IHttpHandler Handler, Textwriter writer, Boolean PreserveForm, Boolean setPreviousPage, VirtualPath Pfad, VirtualPath filePath, String physPath, Ausnahmefehler, String queryStringOverride) +1436

[Httpexception (0x80004005): Erreur d'exécution de la demande pour enfant le gestionnaire 'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'.] System.Web.HttpServerUtility.ExecuteInternal (IHttpHandler-Handler, TextWriter-Writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath-Pfad, VirtualPath-Dateipfad, String physPath, Ausnahmefehler, String queryStringOverride +3428452) System.Web.HttpServerUtility.Execute (IHttpHandler Handler, Textwriter writer, Boolean PreserveForm, Boolean setPreviousPage) +76 System.Web.HttpServerUtility.Ex ecute (IHttpHandler Handler, Textwriter writer, Boolean PreserveForm) +29 System.Web.HttpServerUtilityWrapper.Execute (IHttpHandler Handler, Textwriter writer, Boolean PreserveForm) +24 System.Web.Mvc.Html.ChildActionExtensions.ActionHelper (Htmlhelper Htmlhelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +464 System.Web.Mvc.Html.ChildActionExtensions.Action (HtmlHelper htmlHelper, Zeichenfolge actionName, String controllername, RouteValueDictionary routeValues) +83 System.Web.Mvc.Html.ChildActionExtensions .Action (Htmlhelper Htmlhelper, String action, String controller) +10 ASP._Page_Views_Home_Index_cshtml.Execute() in c: \ Pfad \ My \ Project To \ \ Views \ Home \ Index.cshtml: 14

Wenn dies hilft, verwenden Sie die Lösung Autofac.MVC5 ... Jede Hilfe/Kommentar/Idee ist willkommen.

Bin ich etwas fehlt oder nur die Routen sind in Controller Auflösung impliziert?

UPDATE

Wenn der "Admin" Bereich alle Aktionen gerade gut ausgeführt werden, funktionieren das Plugin und der Admin-Controller in Ordnung.

Wenn ich das Plugin Route kommentieren, wird der Fehler nicht mehr geworfen. Natürlich wird mein Plugin-Controller nicht mehr aufgerufen.

// routes.MapRoute("Route1", 
//  "Admin/Product/{action}/{id}", 
//  new { controller = "Product", action = "Index", area = "Admin", id = UrlParameter.Optional }, 
//  new[] { "Plugin.Controllers" } 
//) 

Also ich denke, das Problem mit dem Plugin Route zurückzuführen ist, das Plug-Controller selbst oder die Art und Weise der Weg von MVC behoben ist?

UPDATE 2

Ich versuchte, meine Routen aller und in @ Html.Action() wie folgt angeben Bereich Kommentar-:

@Html.Action("HomepageProducts", "Product", new { area = "" }) 

Aber MVC ignorieren die Gegend und versuchen, das Plugin zu laden Controller ...

[Httpexception (0x80004005): eine öffentliche Aktion Methode 'Startseite' wurde nicht auf Controller 'gefunden Plugin.Controllers .ProductController‘.]

+0

In Ihrem Controller, 'Plugin.Controllers.ProductController' .. haben Sie eine' ActionResult'-Methode namens 'HomepageProducts', die eine' View' zurückgibt? –

+0

Nein, tue ich nicht. Nur die öffentliche habe diese Aktion –

+0

Ich würde annehmen, dass dies passiert, weil in Ihrer Route.config, speziell für "Route 1". Sie rufen '@ Html.Action (" HomepageProducts "," Produkt ")' was ist Gehen Sie zum 'Product' Controller, der mit der URL übereinstimmt, die Sie in' Route 1' angegeben haben. –

Antwort

0

Ich bin auf jeden Fall sicher, dass ich etwas vermissen, aber ich umgehen mein Problem, indem jede Plugin.ProductController Aktion Routen registriert ich aus dem Admin eines außer Kraft gesetzt habe.

routes.MapRoute("Route1", 
    "Admin/Product/{action}/{id}", 
    new { controller = "Product", action = "Index", area = "Admin", id = UrlParameter.Optional }, 
    new[] { "Plugin.Controllers" } 
) 

//...becomes... 

routes.MapRoute("Product.Action1", 
    "Admin/Product/Action1/{id}", 
    new { controller = "Product", action = "Action1", area = "Admin", id = UrlParameter.Optional }, 
    new[] { "Plugin.Controllers" } 
) 

routes.MapRoute("Product.Action2", 
    "Admin/Product/Action2/{id}", 
    new { controller = "Product", action = "Action2", area = "Admin", id = UrlParameter.Optional }, 
    new[] { "Plugin.Controllers" } 
) 

//... 

Alles funktioniert jetzt gut. Damit kann ich Controller ableiten.

Verwandte Themen