且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET MVC 4区域中正确使用Html.ActionLink

更新时间:2023-02-15 22:23:43

我讨厌回答我自己的问题,但是@Matt Bodily使我走上了正轨.

I hate answering my own question, but @Matt Bodily put me on the right track.

@ Html.Action 方法实际上会调用一个控制器并呈现视图,因此在我的情况下无法创建HTML片段,因为这会导致递归函数调用在***Exception中. @ Url.Action(action,controller,{area ="abc"})确实返回了URL,但是我最终发现提供了 Html.ActionLink 的重载.针对我的情况的更好解决方案:

The @Html.Action method actually invokes a controller and renders the view, so that wouldn't work to create a snippet of HTML in my case, as this was causing a recursive function call resulting in a ***Exception. The @Url.Action(action, controller, { area = "abc" }) does indeed return the URL, but I finally discovered an overload of Html.ActionLink that provided a better solution for my case:

@Html.ActionLink("Admin", "Index", "Home", new { area = "Admin" }, null)

注意: ,在这种情况下,null 表示有效,以匹配正确的签名.

Note: , null is significant in this case, to match the right signature.

文档: @Html.ActionLink(LinkExtensions.ActionLink)

有关此特殊重载的文档:

Documentation for this particular overload:

LinkExtensions.ActionLink(控制器,操作,文本,RouteArgs,HtmlAttributes)

很难找到这些帮助者的文档.如果可能对以后的任何人有帮助,我通常会在应该搜索"LinkExtensions.ActionLink"的情况下倾向于搜索"Html.ActionLink".

It's been difficult to find documentation for these helpers. I tend to search for "Html.ActionLink" when I probably should have searched for "LinkExtensions.ActionLink", if that helps anyone in the future.

仍然将Matt的回答标记为答案.

Still marking Matt's response as the answer.

找到了另一个HTML帮助程序来解决此问题:

Found yet another HTML helper to solve this:

@Html.RouteLink("Admin", new { action = "Index", controller = "Home", area = "Admin" })