BuildUrlFromExpression

cs0*_*815 2 asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

我目前正在看这个:

ASP.NET MVC中的Radchart

但不知道如何处理这段代码:

string url = helper.BuildUrlFromExpression<T>(action);
Run Code Online (Sandbox Code Playgroud)

这是我可以使用的MVC辅助方法吗?任何反馈都将非常感激.谢谢!

基督教

Ole*_*syk 5

我有更好的答案.

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)