2012-09-20 88 views
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); 
    } 

任何意見/建議表示讚賞。

+0

您是否在使用區域?同時顯示自定義'ActionImage'助手的源代碼可能有助於解釋爲什麼它會生成這樣的URL。 –

+0

@DarinDimitrov 沒有使用區域。 已添加Html助手代碼 – user1079925

回答

0

好的, 經過數小時的挫折,我發現斷點沒有被擊中。

原來,一箇舊的組件正在被加載。

檢查生成設置:項目 - >屬性 - > Build標籤

構建輸出路徑設置爲:/斌/ 86 /調試

把它改爲:/ bin中

和斷點被擊中,hrefs顯示正確的路徑。

唷!