如何在自定义路由中使用RenderAction? [英] How to use RenderAction with custom Routing?

查看:66
本文介绍了如何在自定义路由中使用RenderAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始在MVC项目中使用路由,因此我们基本上将RouteLink()替换为ActionLink()-现在我们要替换RenderAction()以添加所需的路由信息​​(主要是路由名称)...

这是我们的映射之一:

 //路由工具route = routes.MapRoute(名称:工具",url:工具/{controller}/{action}",默认值:new {controller ="Home",action ="Index"},命名空间:new [] {"Web.Controllers.Tool"});route.DataTokens ["UseNamespaceFallback"] = false; 

因此,对于RenderAction(控制器,动作),我们掌握了一半的信息,我们需要的是路由名或名称,或者在方法中调用的名称,以便在工具/控制器/动作"中分配工具".

解决方案

您正在混淆两个不相关的想法.授予MVC本身在这里也有些混乱,这就是为什么在MVC 6中,他们放弃了子操作并创建了视图组件".

简单来说,子操作(当您调用 RenderAction 时正在使用的内容)利用了路由基础结构,但它们不是真正的操作,因此通常不会暴露于外界路线.换句话说,内部生成的到达子动作的URL实际上并不重要,只要它到达该动作即可.因此,您不需要您的自定义 Tool 路由即可执行子操作.您只需要一个控制器和一个动作,默认路由就足以使您到达那里.这就是为什么没有 Html.RenderRoute Html.Route 操作与 Html.RenderAction Html.Action .没必要.

现在,路由的工作方式是引入项目中的所有控制器,然后根据路由从最匹配的那个集合中选择一个.通过将控制器分成不同的名称空间,您可以在技术上重用每个名称空间中的控制器名称,但是随后您必须为任何路由指定名称空间,以便将选择范围缩小到正确的控制器/操作对,这就是为什么我认为您如此感到有必要在这里使用自定义路线.我本人并没有真正做到这一点,但是我认为您应该能够将名称空间作为路由值传递:

  @ Html.Action("Action","Controller",新{名称空间="Web.Controllers.Tool"}) 

we are starting to use routes for our MVC project, so we basicly replace ActionLink() with RouteLink() - now we want to replace RenderAction() to add route information we need (mainly the routename)...

Here is one of our mappings:

 // Route für Tools
        route = routes.MapRoute(
            name: "Tool",
            url: "tool/{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" },
            namespaces: new[] { "Web.Controllers.Tool" });
        route.DataTokens["UseNamespaceFallback"] = false;

So with RenderAction(controller, action) we have half of the information, what we need is the routename or name or whatever it is called in the method to assign "tool" in "tool/controller/action".

解决方案

You're confusing two unrelated ideas. Granted MVC itself is a bit confused here as well, which is why in MVC 6, they've dropped child actions and created "view components" instead.

Simply, child actions (which is what you're utilizing when you call RenderAction) utilize the routing infrastructure, but they aren't true actions and aren't typically exposed to the outside worlds as routes. In other words, the URL generated internally to get at the child action doesn't actually matter, as long as it gets to the action. As a result, you don't need your custom Tool route for a child action. You just need a controller and action and the default route will be enough to get you there. This is why there's no Html.RenderRoute or Html.Route actions to correspond with Html.RenderAction and Html.Action. There's no need.

Now, the way routing works is that all the controllers in the project are pull in and then based on the route, one is picked from that set that matches best. By separating out your controllers into different namespaces, you can technically reuse controller names within each namespace, but then you must specify the namespaces for any route in order to narrow the choices down to the right controller/action pair, which is why I think you're feeling the need to use custom routes here. I don't really do this myself, but I think you should be able to pass the namespace as a route value:

@Html.Action("Action", "Controller", new { namespace = "Web.Controllers.Tool" })

这篇关于如何在自定义路由中使用RenderAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆