2011-03-23 64 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); 
    } 
}