在为MVC站点创建插件时,我遇到了有关控制器解析的问题 .
以下是涉及当前上下文的所有路径:
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" }
);
整个解决方案中有3个ProductController类:
-
Plugin.Controllers.ProductController(继承自Admin一个)
-
Admin.Controllers.ProductController
-
Public.Controllers.ProductController
在我的视图“/Views/Home/Index.cshtml”(即没有任何区域的HomeController的“索引”动作)我有以下代码:
@Html.Action("HomepageProducts", "Product")
它指的是 Public.Controllers .ProductController的动作"HomepageProducts" .
MVC查找插件控制器而不是公共控制器,并抛出以下异常:
[HttpException(0x80004005):在控制器'Plugin.Controllers.ProductController'上找不到公共操作方法'HomepageProducts' . ] System.Web.Mvc.Controller.HandleUnknownAction(String actionName)291 System.Web.Mvc.Controller . b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)31 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End()49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult) asyncResult)36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult,Controller controller)12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)22 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End()49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)28 System.Web.Mvc .MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)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):执行子请求失败 . 请查看InnerException以获取更多信息 . ] System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func
1 func)102 System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action)64 System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult)结果)71 System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler,TextWriter writer,Boolean preserveForm,Boolean setPreviousPage,VirtualPath path,VirtualPath filePath,String physPath,Exception error,String queryStringOverride)1436 [HttpException(0x80004005):Erreurd'exécutionde la demande enfant pour le gestionnaire'System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerAsyncWrapper' . ] System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler,TextWriter writer,Boolean preserveForm,Boolean setPreviousPage,VirtualPath path,VirtualPath filePath,String physPath,Exception error, String queryStringOverride)3428452 System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer,Boolean preserveForm,Boolean setPreviousPage)76 System.Web.HttpServerUtility.Execute(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,String actionName,String controllerName,RouteValueDictionary routeValues)83 System .Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,String actionName,String controllerName)10 ASP._Page_Views_Home_Index_cshtml.Execute()在c:\ Path \ To \ My \ Project \ Views \ Home \ Index.cshtml:14
如果这有帮助,解决方案使用Autofac.MVC5 ......欢迎任何帮助/评论/想法 .
我错过了某些东西,或者只有控制器分辨率中隐含的路线?
UPDATE
浏览“管理”区域时,所有操作都执行得很好,插件和管理控制器工作正常 .
如果我评论插件路由,则不再抛出错误 . 当然我的插件控制器不再被调用 .
// routes.MapRoute("Route1",
// "Admin/Product/{action}/{id}",
// new { controller = "Product", action = "Index", area = "Admin", id = UrlParameter.Optional },
// new[] { "Plugin.Controllers" }
// )
所以我想问题是由于插件路由,插件控制器本身或MVC解析路由的方式?
UPDATE 2
我试图取消注释我的所有路由并在@ Html.Action()中指定区域如下:
@Html.Action("HomepageProducts", "Product", new { area = "" })
但MVC忽略该区域并尝试加载插件控制器...
[HttpException(0x80004005):在控制器'Plugin.Controllers.ProductController'上找不到公共操作方法'HomepageProducts' .
1 回答
我绝对肯定我会错过一些东西,但是我通过注册每个Plugin.ProductController动作路径来绕过我的问题,我已经从管理员中覆盖了这些路线 .
现在一切正常 . 有了这个,我就能够得到控制器 .