如何通过Url.Action中的Area?

编程入门 行业动态 更新时间:2024-10-25 22:24:13
本文介绍了如何通过Url.Action中的Area?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Html.ActionLink()中的问题是您不能在其生成的标记内添加其他html内容. 例如,如果您想在文本之外添加图标,例如:

The problem in Html.ActionLink() is that you can't add additional html content inside the tag that it generates. For example, if you want to add an icon besides the text like:

<a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a>

使用Html.ActionLink(),您只能生成:

Using Html.ActionLink(), you can only generate:

<a href="/Admin/Users">Go to Users</a>

因此,要解决此问题,您可以使用Url.Action()仅在标记内生成URL,例如:

So, to resolve this, you can use Url.Action() to generate only the URL inside the tag like:

// Here, Url.Action could not generate the URL "/admin/users". So this doesn't work. <a href="@Url.Action("", "Users", "Admin")"><i class="fa fa-usesr"></i> Go to Users</a> // This works, as we know it but won't pass the Area needed. <a href="@Url.Action("", "Users")"><i class="fa fa-users"></i> Go to Users</a>

那么,如何使用Url.Action()通过区域?

So, how do you pass the Area using Url.Action()?

推荐答案

您可以使用此Url.Action("actionName", "controllerName", new { Area = "areaName" });

也不要忘记添加控制器的名称空间,以避免管理区域控制器名称和站点控制器名称之间的冲突.

Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.

类似这样的东西

public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new[] { "Site.Mvc.Areas.Admin.Controllers" } ); }

更多推荐

如何通过Url.Action中的Area?

本文发布于:2023-11-03 20:40:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Url   Action   Area

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!