簡體   English   中英

如何從視圖調用MVC路由

[英]How to call MVC routes from the View

我遇到的問題是在當前URL上添加URL,例如:Localhost / Config-> Localhost / Config / Profile通過調用@ Html.ActionLink(Resources.General.Achievements,“ Index”,“ Profile”)

我真正想要的是:Localhost /配置文件

我知道使用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" }
            );

我正在嘗試訪問根目錄中的Profile Controller和Index操作。 我同時擁有“配置”區域和“管理區域”,在回撥到根目錄中的控制器時,我試圖避開這些區域。

預先感謝您的任何幫助

使用RouteLink

@Html.RouteLink(Resources.General.Achievements, "Profile", new { id = "1234" })

請注意,由於您的“個人資料”路由上的“ id”參數不是可選的,因此除非您提供它,否則它將不匹配。

處理區域時,如果要從一個區域移動到另一區域或從某個區域移到根中,則可以使用ActionLink重載,該重載允許您指定路線數據並傳遞空字符串(或適當的區域名稱) )進入區域:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM