如何通过Url.Action中的Area? [英] How to pass Area in Url.Action?

查看:142
本文介绍了如何通过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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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