ASP.NET MVC3 HttpStatusCodeResult StatusDescription - 指定的参数超出了有效值的范围

Chr*_*sic 11 asp.net http-status-codes asp.net-mvc-3

出于某种原因,我在尝试返回时遇到错误

new HttpStatusCodeResult(500, statusDescription);

在我的MVC3应用程序中.它因错误而爆炸

Specified argument was out of the range of valid values.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value]
   System.Web.HttpResponse.set_StatusDescription(String value) +4877926
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +785441
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.372 
Run Code Online (Sandbox Code Playgroud)

如果我改变我的结果返回 new HttpStatusCodeResult(500, "statusDescription");

它会正常工作,状态描述是否有某种长度限制?

SLa*_*aks 25

查看源代码或文档,它不能超过512个字符.

  • 几年后回到这个答案,它仍然让我为这个错误是多么愚蠢而生气.即使他们必须这样做,事实上它并不是一个有意义的错误信息,并且加上它并没有快速设置太长的值.ಠ_ಠ (6认同)
  • 我只需要浏览整个 WebAPI 并找到设置 ReasonPhrase 的位置并确保长度小于 512。经过数周的人们对日志中模棱两可的错误消息感到困惑之后。 (3认同)