Asp.net mvc BuildUrlFromExpression将区域添加到Url

Asp.net mvc BuildUrlFromExpression将区域添加到Url,asp.net-mvc,url-routing,html-helper,Asp.net Mvc,Url Routing,Html Helper,我有一个带有管理区的站点,我创建了一个HTML帮助器,帮助我在视图中创建不同大小的图像,如下所示 Html.Image<ImageController>(c => c.DisplayImage(img.Filename, 53, 35), "Product Thumbnail") Html.Image(c=>c.DisplayImage(img.Filename,53,35),“产品缩略图”) 这是我的助手 public static string Image<T&g

我有一个带有管理区的站点,我创建了一个HTML帮助器,帮助我在视图中创建不同大小的图像,如下所示

Html.Image<ImageController>(c => c.DisplayImage(img.Filename, 53, 35), "Product Thumbnail")
Html.Image(c=>c.DisplayImage(img.Filename,53,35),“产品缩略图”)
这是我的助手

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);
    }
public static string Image(这是HtmlHelper帮助程序,表达式报告了问题,但提交的解决方案不是为我编译的。不确定从这里可以得到什么帮助,任何帮助都会很好。

我有更好的答案

    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);
    }
}
公共静态字符串图像(此HtmlHelper帮助程序、表达式操作、int-width、int-height、string-alt)
其中T:控制器
{
var expression=action.Body作为MethodCallExpression;
string actionMethodName=string.Empty;
if(表达式!=null)
{
actionMethodName=expression.Method.Name;
}
字符串url=new UrlHelper(helper.ViewContext.RequestContext,helper.RouteCollection).Action(actionMethodName,typeof(T).Name.Remove(typeof(T).Name.IndexOf(“Controller”)).ToString();
//字符串url=LinkBuilder.BuildUrlFromExpression(helper.ViewContext.RequestContext,helper.RouteCollection,action);
返回string.Format(“”,url,宽度,高度,alt);
}
}

我通过在
RouteValueDictionary
中添加
area=string.Empty
解决了这个问题,虽然这不是最漂亮的解决方案,但效果很好。我决定暂时放弃“又好又漂亮”的帮助程序:(。相反,我使用的是一个标准图像标记,图像url指向我的操作。@Mike,我尝试一下你的建议。