0

I have a site with an admin area and I have created a HTML Helper to help me create images of different sizes in my views, with the following.

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

This is my helper,

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);
    }

The problem I am having is the line string url = LinkExtensions.BuildUrlFromExpression(helper, action); is adding the admin area to the url.

Eg http://localhost:57771/Admin/Image/DisplayImage?....
Instead of http://localhost:57771/Image/DisplayImage?....

I believe it is related to this reported issue, but the workaround submitted is not compiling for me. Not sure where to go from here, any help would be great.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Richard Tasker
  • 244
  • 2
  • 12
  • I've solved this by adding `area = string.Empty` in the `RouteValueDictionary`, which didn't feel like the prettiest solution but it works. – Mikael Östberg Mar 23 '11 at 16:31
  • I have decided to drop the nice and pretty helper for now :(. Instead I am using a standard image tag with the image url pointing to my action. @Mike I'm give your suggestion a try. – Richard Tasker Mar 30 '11 at 08:08

1 Answers1

1

I have better answer!

    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);
    }
}
Oleksandr Fentsyk
  • 5,256
  • 5
  • 34
  • 41