0
我有一個HTML助手:ActionLink的,其產生 'A HREF'MVC3路由 - HREF已經被添加附加的路由
內一個 'IMG',所以這樣的:
Html.ActionImage"Create", "Premises", "~/Images/Views/Premises/LicensableActivitiesButton.png", "Licensable Activities", new { id = "licensableActivitiesImg" })</div>
生成此:
<a id="licensableActivitiesImg" href="/Users/Premises/Create"><img src="/Images/Views/Premises/LicensableActivitiesButton.png" alt="Licensable Activities">
問題是,它將'/ Users /'附加到href的開頭,我不知道爲什麼。這追加也發生在我開始形式以及:
using (@Html.BeginForm("Create", "Premises", FormMethod.Post, new { id = "premisesForm" }))
給出:
<form id="premisesForm" method="post" action="/Users/Premises/Create">
我的路線很簡單:沒有使用
routes.MapRoute(
"",
"Premises/Premises/Create",
new { controller = "Premises", action = "Create" }
);
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
地區。
HTML輔助代碼:
public static MvcHtmlString ActionImage(this HtmlHelper html, string action, string
Controller, string imagePath, string altText, object htmlAttributes, object routeValues)
{
var url = new UrlHelper(html.ViewContext.RequestContext);
// build the <img> tag
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", url.Content(imagePath));
imgBuilder.MergeAttribute("alt", altText);
string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
// build the <a> tag
var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues));
anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(anchorHtml);
}
任何意見/建議表示讚賞。
您是否在使用區域?同時顯示自定義'ActionImage'助手的源代碼可能有助於解釋爲什麼它會生成這樣的URL。 –
@DarinDimitrov 沒有使用區域。 已添加Html助手代碼 – user1079925