Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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/0/asp.net-mvc/17.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# MVcHtmlString中的堆栈溢出异常_C#_Asp.net Mvc_Mvchtmlstring_Asp.net Mvc Helpers - Fatal编程技术网

C# MVcHtmlString中的堆栈溢出异常

C# MVcHtmlString中的堆栈溢出异常,c#,asp.net-mvc,mvchtmlstring,asp.net-mvc-helpers,C#,Asp.net Mvc,Mvchtmlstring,Asp.net Mvc Helpers,我已经创建了自己的Html帮助程序,它将红色星号添加到任何必填字段中 它成功地与这两种方法配合使用 @Html.myLabelFor(model => model.Description) //and @Html.myLabelFor(model => model.Description, new { /*stuff*/ }) 但是,一些代码行如下所示 @Html.myLabelFor(model => model.Description, "Deletion Reason"

我已经创建了自己的Html帮助程序,它将红色星号添加到任何必填字段中

它成功地与这两种方法配合使用

@Html.myLabelFor(model => model.Description)
//and
@Html.myLabelFor(model => model.Description, new { /*stuff*/ })
但是,一些代码行如下所示

@Html.myLabelFor(model => model.Description, "Deletion Reason", new { /*stuff*/ })
我的方法不是设计用来处理3个参数的,所以我添加了一个调用者来处理3个参数

public static MvcHtmlString myLabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
     Expression<Func<TModel, TValue>> expression, string labelText, Object htmlAttributes)
  {
      return myLabelFor(html, expression, labelText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  }    
公共静态MvcHtmlString myLabelFor(此HtmlHelper html,
表达式、字符串labelText、对象htmlAttributes)
{
返回myLabelFor(html、表达式、labelText、HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}    
下面是正常工作的其他方法(包括internal,它包含所有必要的代码,我将其结构用作参考)

公共静态MvcHtmlString myLabelFor(此HtmlHelper html,
表达式,IDictionary htmlAttributes)
{
返回LabelHelper(html,ModelMetadata.FromLambdaExpression(表达式,html.ViewData),
GetExpressionText(表达式),null,htmlAttributes);
}
公共静态MvcHtmlString myLabelFor(此HtmlHelper html,
表达式(表达式)
{
返回LabelHelper(html,ModelMetadata.FromLambdaExpression(表达式,html.ViewData),
GetExpressionText(表达式),null);
}
公共静态MvcHtmlString myLabelFor(此HtmlHelper html,
表达式、对象(属性)
{
返回myLabelFor(html,表达式,HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
//将其结构用作参考
内部静态MvcHtmlString LabelHelper(HtmlHelper html、ModelMetadata元数据、字符串htmlFieldName、,
字符串labelText=null,IDictionary htmlAttributes=null)
从逻辑上讲,我希望参数labelText会从上面的代码行中获取一个值“deletition Reason”。然而,它却在我的3参数方法中抛出了一个StackOverflowException。是模糊的,没有帮助,并且正在使用

Expression<Func<TModel, string>> expression instead of my Expression<Func<TModel, TValue>> expression
表达式而不是我的表达式
我不明白我做错了什么。在这一点上,我只能想到“在参数起作用之前对其进行调整”,但我希望有更优雅的解决方案来解决这个问题


PS:请告诉我我的internal helper代码是否有助于解决问题。

您在第一次重载时遇到异常,因为该方法正在递归地调用自身,并一直这样做,直到执行堆栈溢出。你需要改变,而不是自称

return myLabelFor(html, 
                  expression,
                  labelText,
                  HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

根据您的评论,使用
return myLabelFor(…)
的第四个重载没有引发异常的原因是它调用了第二个重载,而第二个重载又调用了
return LabelHelper(…)


我建议您将第4个重载更改为直接调用
LabelHelper()
,并将所有公共重载更改为显式调用
LabelHelper()
,传递所有4个参数,这是内置的`HtmlHelper扩展方法使用的模式(您可以查看)

,因为您的第一个
myLabelFor()
方法正在调用自身(而不是
LabelHelper()
),它反过来又调用自身,反过来又调用自身,依此类推。您的第一个
myLabelFor方法调用自身@DavidG您是指带有字符串labelText的方法吗?是的,就是那个方法,它递归地调用自己。@DavidG但是大块中带有html、表达式和htmlAttributes的方法呢,它也返回myLabelFor?它没有抛出异常,这就是为什么我决定使用它而不是Label helper,因为更改为LabelHelper会导致错误。非常感谢Stephen!它可以工作,现在我已经了解了它的原因,以及一些有趣的源代码。我接受你的回答!
return myLabelFor(html, 
                  expression,
                  labelText,
                  HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
return LabelHelper(html,
                   ModelMetadata.FromLambdaExpression(expression, html.ViewData),
                   ExpressionHelper.GetExpressionText(expression),
                   labelText,
                   HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));