@Html.动作和控制器具有相同的名称



在为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

In my view "/Views/Home/Index。(即没有任何区域的HomeController的"Index"动作)我有以下代码:

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

它指的是Public的"HomepageProducts"操作。控制器.ProductController .

MVC查找插件控制器而不是公共控制器,并抛出以下异常:

[HttpException (0x80004005):在控制器'Plugin.Controllers.ProductController上没有找到公共操作方法'HomepageProducts'"。)System.Web.Mvc.Controller。HandleUnknownAction(String actionName) +291System.Web.Mvc.Controller。b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +31System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase. end () +49System.Web.Mvc.Controller。EndExecuteCore(IAsyncResult asyncResult) +36System.Web.Mvc.Controller。b__15(IAsyncResult asyncResult, Controller Controller) +12System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase. end () +49System.Web.Mvc.Controller。endexexecute (IAsyncResult asyncResult) +26System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController。endexexecute (IAsyncResult asyncResult) +10System.Web.Mvc.MvcHandler。b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase. end (System.Web.Mvc.MvcHandler。EndProcessRequest(IAsyncResult asyncResultSystem.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler。EndProcessRequest(IAsyncResult result) +9System.Web.Mvc灵活;>c__DisplayClassa.b__9 () + 22System.Web.Mvc灵活;>c__DisplayClass4.b__3 () + 10System.Web.Mvc.ServerExecuteHttpHandlerWrapper。

[HttpException (0x80004005):执行子请求失败。]请查看InnerException获取更多信息。]System.Web.Mvc.ServerExecuteHttpHandlerWrapper。Wrap(Func ' 1 Func) +102System.Web.Mvc.ServerExecuteHttpHandlerWrapper。Wrap(动作动作)+64System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper。EndProcessRequest(IAsyncResult result) +71System.Web.HttpServerUtility。ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setprevioupage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String querystrinoverride) +1436

[HttpException (0x80004005): error d' excution de la demande enfant pour le gestionnaire 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]System.Web.HttpServerUtility。ExecuteInternal(IHttpHandler handler, textwwriter, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String querystrinoverride) +3428452System.Web.HttpServerUtility。执行(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setprevioupage) +76System.Web.HttpServerUtility。执行(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +29System.Web.HttpServerUtilityWrapper。执行(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +24System.Web.Mvc.Html.ChildActionExtensions。ActionHelper(HtmlHelper HtmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter TextWriter) +464System.Web.Mvc.Html.ChildActionExtensions。Action(HtmlHelper HtmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +83System.Web.Mvc.Html.ChildActionExtensions。Action(HtmlHelper HtmlHelper, String actionName, String controllerName) +10ASP._Page_Views_Home_Index_cshtml.Execute() in c:PathToMyProjectViewsHomeIndex.cshtml:14

如果这有帮助,解决方案使用autoface . mvc5…欢迎任何帮助/评论/想法。

是我遗漏了什么,还是只有路由隐含在控制器的分辨率?

当浏览"管理"区域时,所有动作都执行得很好,插件和管理控制器工作正常。

如果我注释了插件路由,错误不再被抛出。当然,我的插件控制器不再被调用。

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

所以我猜这个问题是由于插件路由,插件控制器本身或路由由MVC解决的方式?

更新2

我试图取消所有路由的注释,并在@Html.Action()中指定区域,如下所示:

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

但是MVC忽略了这个区域,并尝试加载插件控制器…

[HttpException (0x80004005):在控制器插件上没有找到公共操作方法'HomepageProducts'。控制器.ProductController"。)

我肯定我错过了一些东西,但我通过注册每个插件绕过了我的问题。ProductController的动作路由我已经从Admin中重写了。

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" }
)
//...

现在一切正常。有了这个,我就可以派生控制器了。

相关内容

  • 没有找到相关文章

最新更新