BuildUrlFromExpression将区域添加到Url

时间:2011-03-23 15:54:45

标签: asp.net-mvc url-routing html-helper

我有一个带有管理区域的网站,我创建了一个HTML帮助程序,以帮助我在视图中创建不同大小的图像,具体如下。

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);正在将管理区域添加到网址。

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

我认为它与this报告的问题有关,但提交的解决方法并非针对我进行编译。不知道从哪里开始,任何帮助都会很棒。

1 个答案:

答案 0 :(得分: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);
    }
}
相关问题