开发者

BuildUrlFromExpression Adding Area to Url

开发者 https://www.devze.com 2023-02-19 19:46 出处:网络
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.

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 = LinkExte开发者_开发知识库nsions.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.


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

精彩评论

暂无评论...
验证码 换一张
取 消