我正在使用 ASP.Net Core 5 MVC Visual Studio 2019。
我正在構建一個 buildcrumb 路徑。
我的代碼是 -
var breadcrumb = new HtmlContentBuilder()
.AppendHtml("<ol class='breadcrumb'><li>")
.AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.Name.Titleize(),ob.breadcrumbmap.YFVCList.Action, ob.breadcrumbmap.YFVCList.Controller))
.AppendHtml("</li>"
if (controllerName.ToLower() != ob.breadcrumbmap.YFVCList.Controller.ToLower())
{
breadcrumb.AppendHtml("<li class='breadcrimb-item'>")
.AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.YFVC.Name.Titleize(), System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action "/1"), ob.breadcrumbmap.YFVCList.YFVC.Controller))
.AppendHtml("</l>");
}
我從放入物件的 JSON 檔案中獲取資訊。-
string jsonData = File.ReadAllText("BreadcrumbMap.json");
Rootobject ob = JsonSerializer.Deserialize<Rootobject>(jsonData);
ob.breadcrumbmap.YFVCList.YFVC.Action= "診所"
但我需要在末尾附加一個 id,所以我使用 "/1" 和 UrlDecode 字串。
System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action "/1")
但是,當我突出顯示它顯示的面包屑時 -
/Clinic/1 而不是 /Clinic/1。
我以為解碼會擺脫它?
撞墻我的頭。
uj5u.com熱心網友回復:
你似乎誤解了這個HTML.ActionLink方法。根據您的代碼和描述,我認為您想在此模板中生成一條路線:
{ControllerName/ActionName/Routevalue}
在您的代碼中,您使用此覆寫方法:
// Summary:
// Returns an anchor (<a>) element that contains a URL path to the specified action.
//
// Parameters:
// helper:
// The Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper instance this method extends.
//
// linkText:
// The inner text of the anchor element. Must not be null.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// Returns:
// A new Microsoft.AspNetCore.Html.IHtmlContent containing the anchor element.
public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName)
{
throw null;
}
System.Web.HttpUtility.UrlDecode(ob.breadcrumbmap.YFVCList.YFVC.Action "/1")被認為是一個完整的動作名稱而不是action/route,所以它會轉換/為/。
您可以使用此覆寫方法:
public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
{
throw null;
}
像這樣寫你的代碼:
.AppendHtml(helper.ActionLink(ob.breadcrumbmap.YFVCList.YFVC.Name.Titleize(), ob.breadcrumbmap.YFVCList.YFVC.Action, ob.breadcrumbmap.YFVCList.YFVC.Controller), new { id=2 }))
然后它會生成這樣的url:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/534575.html
