带有路由名称的Html.RenderAction [英] Html.RenderAction with Route Name
问题描述
我正在尝试在控制器操作中使用Route属性命名(例如: [Route(Name = Abc123)]
),并且同样能够调用可以通过 Html.RenderAction
来实现,但这似乎不支持路由名称。我认为这是因为路由名称仅保留用于通过HTTP请求的名称,而不能直接调用,但是对于MVC来说有些新,所以我很茫然。
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. 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.
我完全使用MVC属性路由,而我没有配置其他路由。看来我必须定义路由名称,并且路由名称必须匹配操作方法名称。但是,这样做时,我尝试命名多个 Index
时出现命名冲突。
I'm using MVC Attribute Routing entirely, and I do not have routes configured otherwise. 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
.
I我基本上是在尝试支持多个局部视图,每个局部视图都有自己的控制器,这些视图可用作我网站上的插件/小部件。因此理想情况下,每个人都会有一个名为 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
.
您对我如何保持相同有建议吗?命名?这使我可以调用 Html.RenderAction( Index,[ControllerName],[Model])
而无需更改渲染名称。
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一起使用,只需要确保属性中的操作名称是实际名称即可:
You can use attribute routing with Html.RenderAction just you have to make sure that the action name in attribute is actual Name :
[Route("Home/About")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
在HTML中,您可以拥有
and in HTML you can have
@ {Html.RenderAction( About, home);}
@{Html.RenderAction("About", "home");}
它将正常工作。
这篇关于带有路由名称的Html.RenderAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!