Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有路径值的辅助对象的C#扩展方法_C#_Asp.net Mvc_Asp.net Mvc Routing_Html Helper - Fatal编程技术网

具有路径值的辅助对象的C#扩展方法

具有路径值的辅助对象的C#扩展方法,c#,asp.net-mvc,asp.net-mvc-routing,html-helper,C#,Asp.net Mvc,Asp.net Mvc Routing,Html Helper,我正在尝试为带有图像的ActionLink助手创建一个扩展方法 这是我的扩展方法: public static class MyHelpers { public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName) { var urlHelper = new UrlHelper(html.ViewContex

我正在尝试为带有图像的
ActionLink
助手创建一个扩展方法

这是我的扩展方法:

public static class MyHelpers
    {
        public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
        {
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);

            string imgUrl = urlHelper.Content(imgSrc);
            TagBuilder imgTagBuilder = new TagBuilder("img");
            imgTagBuilder.MergeAttribute("src", imgUrl);
            string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);

            string url = urlHelper.Action(actionName);

            TagBuilder tagBuilder = new TagBuilder("a")
            {
                InnerHtml = img
            };

            tagBuilder.MergeAttribute("href", url);

            return tagBuilder.ToString(TagRenderMode.Normal);
        }
    }
我试着这样使用它:

@Html.ActionLinkWithImage("Images/del.png", "Delete", new { id = item.ItemID});
但是我的扩展方法没有“路由值”。如何实现这一点?

如下所示:

public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName, object routeValues)
    {

    //Your code ...

    string url = urlHelper.Action(actionName, routeValues);

    }

向方法中添加对象参数

, object routeData) 
并将其传递给UrlHelper

new UrlHelper(new RequestContext(html.ViewContext.HttpContext, routeData))

您使用两个参数定义扩展方法,但使用三个参数调用它。。。那没有道理,变得冷嘲热讽是没有意义的。既然你“显然”注意到了你的错误,那么为什么首先要问这个问题呢?问题是/曾经是如何实现路由的东西。我不是在说任何编译器或运行时错误。事实上,我根本没有提到“错误”。你在说什么?还是没必要被唤醒。我的观点是,很明显,声明中缺少了一个参数。要解决此问题,应添加该参数。因为你“显然”知道你想用三个参数调用这个方法,我不明白你为什么不添加第三个参数,而不是问一个愚蠢的问题。如果不“告诉”如何处理参数,仅仅在方法中添加一个参数是不行的。问题基本上是关于第三个参数“tel”是什么。