开发者

My own HtmlHelper don't work in Asp.Net MVC 2

I have HtmlHelper in ASP.NET MVC 1. Now I wont to migrate to ASP.NET MVC 2, but this helper don't work =(


public static string Image(this HtmlHelper helper, string url, string alt)
{
    return string.Format("<img src=\"{0}\" alt=\"{1}\" />", url, alt);
}

public static string ImageLink<T>(this HtmlHelper helper, Expression<Action<T>> linkUrlAction, string imageUrlPath, string altText)
    where T : Controller
{
    string linkUrl = helper.BuildUrlFromExpression(linkUrlAction);//compile time error
    string img = helper.Image(imageUrlPath, altText);

    string outputUrl = string.Format(@"<a href='{0}'>{1}</a>", linkUrl, img);
    return outputUrl;
}

Error: 'System.Web.Mvc.HtmlHelper' does not co开发者_C百科ntain a definition for 'BuildUrlFromExpression'

How I can fix this error?


Have you referenced the MVC Futures binaries in your project?

Perhaps your using Microsoft.Web.Mvc; was removed or modified in the upgrade from v1 to v2.

The method you're wanting to use is in:

 Microsoft.Web.Mvc.LinkBuilder


The code you are looking for is in the MVCFutures assembly.

However, you could do this with the standard libraries instead by taking the actual url as a string, building it with the UrlHelper on the page rather than using the expression. You do lose the strong typing of the action doing it this way, though. Note, that it doesn't need to be generic if you do this.

public static string ImageLink( this HtmlHelper helper,
                                string linkUrl,
                                string imageUrlPath,
                                string altText )
{
    string img = helper.Image(imageUrlPath, altText);

    string outputUrl = string.Format(@"<a href='{0}'>{1}</a>", linkUrl, img);
    return outputUrl;
}

<%= Html.ImageLink( Url.Action( "action", "controller" ),
                    Url.Content( "~/content/images/button.png" ),
                    "Click Me" ) %>


I have a better answer!

public static class ImageResultHelper
{
    public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action, int width, int height)
            where T : Controller
    {
        return ImageResultHelper.Image<T>(helper, action, width, height, "");
    }

    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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜