2011-03-23 67 views
0

我有一個具有管理員區域的網站,我創建了一個HTML助手來幫助我在視圖中創建不同大小的圖像,具體如下。BuildUrlFromExpression將區域添加到Url

Html.Image<ImageController>(c => c.DisplayImage(img.Filename, 53, 35), "Product Thumbnail") 

這是我的助手,

public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action,string alt) where T : Controller 
    { 
     string url = LinkExtensions.BuildUrlFromExpression(helper, action); 
     return string.Format("<img src=\"{0}\" alt=\"{1}\" />", url, alt); 
    } 

我遇到的問題是該行string url = LinkExtensions.BuildUrlFromExpression(helper, action);是增加了管理區的URL。

http://localhost:57771/Admin/Image/DisplayImage?....
而不是http://localhost:57771/Image/DisplayImage?....

我認爲這是關係到this報告的問題,但提出的解決方法是不編制適合我。不知道該從哪裏出發,任何幫助都會很棒。

+0

我已經在'RouteValueDictionary',這並不覺得自己是最漂亮的解決方案中加入'面積= string.Empty'解決了這個,但它的工作原理。 – 2011-03-23 16:31:52

+0

我決定放棄現在很漂亮的漂亮幫手:(。相反,我正在使用標準圖片標記,圖片網址指向我的動作。@Mike我給你的建議一試。 – 2011-03-30 08:08:29

回答

1

我有更好的回答!

public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action, int width, int height, string alt) 
      where T : Controller 
    { 
     var expression = action.Body as MethodCallExpression; 
     string actionMethodName = string.Empty; 
     if (expression != null) 
     { 
      actionMethodName = expression.Method.Name; 
     } 
     string url = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection).Action(actionMethodName, typeof(T).Name.Remove(typeof(T).Name.IndexOf("Controller"))).ToString();   
     //string url = LinkBuilder.BuildUrlFromExpression<T>(helper.ViewContext.RequestContext, helper.RouteCollection, action); 
     return string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" alt=\"{3}\" />", url, width, height, alt); 
    } 
}