MVC Futures 具有区域和重复控制器名称的强类型 RenderAction
我正在尝试使用 MVC Futures 库中的强类型性质的 Html.RenderAction
我在我的主 HomeController (区域 =“”)上有一个导航操作,我试图从我的 Site.Master 调用
<% Html.RenderAction<HomeController>(x=>x.Navigation()); %>
它,效果很好直到我将另一个 HomeController 添加到我的管理便携式区域。然后我开始出现以下错误:
发现多种类型匹配 控制器名为“Home”。这个可以 如果服务于此的路线会发生 请求未指定名称空间 搜索匹配的控制器 的请求。如果是这样的话, 通过调用注册此路由 'MapRoute' 方法的重载 采用“命名空间”参数。
“Home”的请求已找到 以下匹配控制器: 区域.Admin.Controllers.HomeController Web.Controllers.HomeController
我能够通过使用以下非 mvccontrib 强类型 RenderAction 方法解决该问题。我不想在页面上使用这种非强类型方法,有没有办法让 mvccontrib 方法接受一个区域,或者让它们知道根据指定的控制器的路由或命名空间生成区域。
<% Html.RenderAction("Navigation", "Home", new { area = "" }); %>
我已经添加到我的 global.asax.cs 文件中,以支持具有默认区域的多个控制器,如下所示,并且我还验证了这是我的控制器的正确命名空间。
_routeCollection.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Web.Controllers" });
I'm trying to use an Html.RenderAction in a strongly typed nature from the MVC Futures library
I have a Navigation action on my primary HomeController (area = "") that I'm trying to call from my Site.Master
<% Html.RenderAction<HomeController>(x=>x.Navigation()); %>
This worked great until I added another HomeController to my Admin portable area. Then I started to the following error:
Multiple types were found that match
the controller named 'Home'. This can
happen if the route that services this
request does not specify namespaces to
search for a controller that matches
the request. If this is the case,
register this route by calling an
overload of the 'MapRoute' method that
takes a 'namespaces' parameter.The request for 'Home' has found the
following matching controllers:
Areas.Admin.Controllers.HomeController
Web.Controllers.HomeController
I was able to resolve the issue by using the following non-mvccontrib strongly typed RenderAction method. I would rather not have this non-strongly typed method on the page, is there a way to make the mvccontrib methods accept an area, or for them to know to generate the area based on routes or the namespace of the Controller being specified.
<% Html.RenderAction("Navigation", "Home", new { area = "" }); %>
I have already added to my global.asax.cs file to support the multiple controllers with a default area as shown below, and I've also verified that this is the correct namespace for my controllers.
_routeCollection.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Web.Controllers" });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然目前 futures 项目不支持这一点,或者他们从 Futures v1.0 开始改变了它。我使用 Reflector 反汇编了 futures 库,找到了 RenderAction 方法的源代码,并对其进行了修改,以解析控制器命名空间之外的区域,并在处理之前将其添加到 RouteValues 中。我假设如果我希望 ActionLink 的行为方式相同,我可能也必须对 ActionLink 执行类似的操作。
这是我使用的代码示例,希望这可以帮助将来的其他人。我可能会创建新方法,表明它将解析这些区域,而不是像我迄今为止所做的那样重载
Apparently there is not support for this in the futures project at this time, or they changed it since Futures v1.0. I used reflector to disassemble the futures library and found the source for the RenderAction method and modified it to parse the area out of the Controller's namespace and add it into the RouteValues before it processes. I'm assuming that I'll likely have to do something similar for ActionLink's as well if I want them to behave in the same way.
Here is a sample of the code that I used, so hopefully this can help someone else in the future. I will likely create new methods indicating that it will parse the areas, rather than an overload as I have thus far
为了支持 MVC 中的多个控制器,您需要向默认路由添加命名空间。
有关更多详细信息,请参阅此答案:问题2627699
In order to support multiple controllers in MVC you will need to add a namespace to your default route.
See this answer for more detail: Question 2627699