简体   繁体   English

带路由名称的Html.RenderAction

[英]Html.RenderAction with Route Name

I am trying to utilize the Route attribute naming (ex: [Route(Name = "Abc123")] ) in a controller action, and likewise be able to call that via Html.RenderAction , but it appears that this does not support route names. 我试图在控制器操作中使用Route属性命名(例如: [Route(Name = "Abc123")] ),同样能够通过Html.RenderAction调用它,但看起来这不支持路由名称。 I assume this is because route names are reserved only for ones requested via HTTP and not called directly, but somewhat new to MVC so I'm at a loss. 我认为这是因为路由名称仅保留给通过HTTP请求的路由,而不是直接调用,但对MVC来说有点新,所以我很茫然。

I'm using MVC Attribute Routing entirely, and I do not have routes configured otherwise. 我完全使用MVC属性路由,否则我没有配置路由。 It seems that I must define the route name, and the route name has to match the action method name. 似乎我必须定义路由名称,并且路由名称必须与操作方法名称匹配。 In doing so, however, I am getting naming conflicts when I try to name more than one Index . 但是,在这样做时,当我尝试命名多个Index时,我会收到命名冲突。

I'm basically trying to support multiple partial views, each having their own controller, which serve as plugins/widgets on my site. 我基本上试图支持多个局部视图,每个视图都有自己的控制器,在我的网站上充当插件/小部件。 So ideally each would have an action called Index . 理想情况下,每个人都会有一个名为Index的动作。

Do you have a recommendation on how I can maintain the same naming? 你有关于如何保持相同命名的建议吗? This allows me to call Html.RenderAction("Index", [ControllerName], [Model]) without the render name changing. 这允许我调用Html.RenderAction("Index", [ControllerName], [Model])而不更改渲染名称。

You can use attribute routing with Html.RenderAction just you have to make sure that the action name in attribute is actual Name : 您可以使用Html.RenderAction属性路由,只需确保属性中的操作名称是实际名称:

 [Route("Home/About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

and in HTML you can have 在HTML中你可以拥有

@{Html.RenderAction("About", "home");} @ {Html.RenderAction(“关于”,“家”);}

It will work fine. 它会工作正常。

You should be able to do what you are trying to do with attribute routing. 您应该可以使用属性路由执行您要执行的操作。 If you are trying to render a partial view you should be using RenderPartial instead of RenderAction. 如果您尝试渲染局部视图,则应使用RenderPartial而不是RenderAction。

Are you doing this between areas? 你是在区域之间做这个吗? I believe there are some gotcha's with making that work correctly. 我相信有一些问题可以让它正常工作。

Can you please post example Controller code and RouteConfig? 您能否发布示例Controller代码和RouteConfig?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM