Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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# 扩展HtmlHelper类asp.net mvc4_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 扩展HtmlHelper类asp.net mvc4

C# 扩展HtmlHelper类asp.net mvc4,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我在扩展HtmlHelper类以呈现图像标记时遇到问题 我刚刚写了这个代码 namespace Mace_CrmSystem { public static class ExtendedHelper { public static TagBuilder HaidarImage(this HtmlHelper instance, string src) { TagBuilder inst = new TagBuilder("img");

我在扩展HtmlHelper类以呈现图像标记时遇到问题

我刚刚写了这个代码

namespace Mace_CrmSystem
{
    public static class ExtendedHelper
    {
        public static TagBuilder HaidarImage(this HtmlHelper instance, string src) {
            TagBuilder inst = new TagBuilder("img");
            inst.MergeAttribute("src", src);
            return inst;     
        }
    }
}
@Html.HaidarImage("http://haidar.ws/wp-content/uploads/2014/07/ipaduse.jpg");
在索引视图中,我编写了以下代码

namespace Mace_CrmSystem
{
    public static class ExtendedHelper
    {
        public static TagBuilder HaidarImage(this HtmlHelper instance, string src) {
            TagBuilder inst = new TagBuilder("img");
            inst.MergeAttribute("src", src);
            return inst;     
        }
    }
}
@Html.HaidarImage("http://haidar.ws/wp-content/uploads/2014/07/ipaduse.jpg");
但问题是,当视图呈现标记时,它不会将其呈现为Hrml标记,而是将其呈现为普通文本,因此结果如下

 <img src="http://haidar.ws/wp-content/uploads/2014/07/ipaduse.jpg"></img>;
另一个问题是,我试图在web.config页面中添加名称空间,以便在所有页面上都可用,但intellisense在我在视图页面本身显式声明扩展方法之前,不会显示该扩展方法


因此,请任何人帮助我解决我的问题。

您必须像这样返回
IHtmlString
,以便将其转换为Html:

 public static IHtmlString HaidarImage(this HtmlHelper instance, string src) 
 {

    TagBuilder inst = new TagBuilder("img");
    inst.MergeAttribute("src", src);
    return MvcHtmlString.Create(inst.ToString(TagRenderMode.SelfClosing));   
  }

您必须像这样返回
IHtmlString
,以便将其呈现为Html格式:

 public static IHtmlString HaidarImage(this HtmlHelper instance, string src) 
 {

    TagBuilder inst = new TagBuilder("img");
    inst.MergeAttribute("src", src);
    return MvcHtmlString.Create(inst.ToString(TagRenderMode.SelfClosing));   
  }

您应该从HtmlHelper扩展方法返回一个
IHtmlString
,这样输出就不会是HTML编码的,因为它是不应该再次编码的HTML标记。比如说

public static IHtmlString HaidarImage(this HtmlHelper instance, string src) 
{
    TagBuilder inst = new TagBuilder("img");
    inst.MergeAttribute("src", src);
    return new HtmlString(inst.ToString(TagRenderMode.SelfClosing));   
}

要修复在所有视图中可用的方法,您需要将类的名称空间添加到视图文件夹内的web.config中。

您应该从HtmlHelper扩展方法返回一个
IHtmlString
,以便输出不是HTML编码的,因为它是不应该再次编码的HTML标记。比如说

public static IHtmlString HaidarImage(this HtmlHelper instance, string src) 
{
    TagBuilder inst = new TagBuilder("img");
    inst.MergeAttribute("src", src);
    return new HtmlString(inst.ToString(TagRenderMode.SelfClosing));   
}

要修复在所有视图中可用的方法,您需要将类的命名空间添加到views文件夹内的web.config中。

我不确定您为什么要从方法返回
TagBuilder
,您应该返回,请尝试以下操作:-

public static MvcHtmlString HaidarImage(this HtmlHelper helper, string src)
{
    TagBuilder tag = new TagBuilder("img");
    tag.Attributes.Add("src", src);
    return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}

为什么我们需要
MvcHtmlString
,您可以阅读。

我不确定您为什么从方法返回
TagBuilder
,您应该返回,请尝试以下操作:-

public static MvcHtmlString HaidarImage(this HtmlHelper helper, string src)
{
    TagBuilder tag = new TagBuilder("img");
    tag.Attributes.Add("src", src);
    return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}

为什么我们需要
MvcHtmlString
,您可以阅读。

将方法更改为
public static MvcHtmlString HaidarImage(…)
并添加
返回MvcHtmlString.Create(inst.ToString())
将方法更改为
公共静态MvcHtmlString HaidarImage(..)
并添加
返回MvcHtmlString.Create(inst.ToString())非常好的答案,非常感谢,这解决了我的问题,但我在pluralSight中发现了一个课程,该课程描述了使用TagBuilder类进行扩展的过程,与我编写代码的过程完全相同,因此我不知道在发布新版本的mvc之前,以前版本的mvc中是否使用过该方法。非常好的答案,非常感谢,这解决了我的问题,但我在pluralSight中发现了一个教训,它描述了使用TagBuilder类进行扩展的过程,与我编写代码的过程完全相同,因此我不知道在发布新版本的mvc之前,以前版本的mvc中是否已经使用了该方法。