C# 如何从视图中调用MVC路由

C# 如何从视图中调用MVC路由,c#,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,我在当前URL上添加URL时遇到问题,例如: Localhost/Config->Localhost/Config/Profile 通过调用@Html.ActionLink(Resources.General.aclements,“Index”,“Profile”) 我真正想要的是:Localhost/Profile 我知道使用MVC路由是可能的,我只是不知道如何从视图中调用它们。我使用了Html.BeginRouteForm,但提交时不会触发此路由 这是我的路线配置 routes.MapRou

我在当前URL上添加URL时遇到问题,例如: Localhost/Config->Localhost/Config/Profile 通过调用@Html.ActionLink(Resources.General.aclements,“Index”,“Profile”)

我真正想要的是:Localhost/Profile

我知道使用MVC路由是可能的,我只是不知道如何从视图中调用它们。我使用了Html.BeginRouteForm,但提交时不会触发此路由

这是我的路线配置

routes.MapRoute(
            name: "Profile",
            url: "Profile/{id}",
            defaults: new { controller = "Profile", action = "Index" },
            namespaces: new[] { "Cobalt.Controllers" }
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Cobalt.Controllers" }
            );
我正在尝试访问根目录中的概要文件控制器和索引操作。我有配置和管理两个区域,在回拨根目录中的控制器时,我试图从中逃离

提前感谢您的帮助

使用:


请注意,由于您的“id”参数在配置文件路由上未声明为可选,因此除非您提供它,否则它将不匹配。

在处理区域时,如果您想从一个区域移动到另一个区域或从一个区域移动到根目录,可以使用ActionLink重载,它允许您指定路由数据并传递空字符串(或适当的区域名称)进入区域:

@Html.ActionLink(Resources.General.Achievements, "Index", "Profile", new { area = string.Empty })

您定义的当前路由是什么?请指定您的控制器和操作。然后指定要匹配的url。我为您更新了我的问题。您想要
@Html.RouteLink()
:两种解决方案都有效,但如果可能的话,我希望避免定义多条路线只是为了穿越区域。我发现我的问题更多的是围绕区域,而不是路线。不过,谢谢!
@Html.ActionLink(Resources.General.Achievements, "Index", "Profile", new { area = string.Empty })