c# - ArgumentOutOfRangeException , IIS express on 2012 server

标签 c# asp.net-mvc

我的所有 Controller 操作都呈现良好,除了一个。

我找到了与 IIS Express 相关的其他解决方案,但这是在服务器 2012 上运行的。

这是 Controller Action :

public ActionResult Index()
    {

        try
        {
            var viewModels = GetHostInfoViewModelList();
            return View(viewModels);
        }
        catch (Exception ex)
        {
            ex.ReportError();
            return new HttpStatusCodeResult(404,$"{ex.Message}|{ex.InnerException}");

        }

    }

这是 GetHostInfoViewModelList:

  public IEnumerable<ViewModelHostInfo> GetHostInfoViewModelList()
    {
        using (var db = new WINCMUEntities())
        {
            try
            {
                //join host info with sleep status
                var sleepRecords = db.SleepTrackings.ToList();
                var hostInfo = db.WINCMU_HostInfo.ToList();
                var viewModels = new List<ViewModelHostInfo>();
                hostInfo.ForEach(x =>
                {
                    viewModels.Add(new ViewModelHostInfo()
                    {
                        HostName = x.HostName ?? "Not Found",
                        Id = x.ID,
                        newsystem = x.newsystem,
                        Zone = x.Zone ?? "Not Found",
                        IsSleeping = sleepRecords.FirstOrDefault(s => s.HostName.ToLower() == x.HostName.ToLower())
                                         ?.IsCurrentlySleeping ?? false,
                        IP_address = x.IP_address ?? "Not Found",
                        ReportingArea = x.ReportingArea ?? "Not Found",
                        agent_active = x.agent_active,
                        date_added = x.date_added,
                        is_agent = x.is_agent
                    });
                });

                return viewModels;
            }
            catch (Exception ex)
            {
                ex.ReportError();
                throw;
                //return new List<ViewModelHostInfo>();
            }
        }
    }

这是完整的错误文本:

[ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:值] System.Web.HttpResponse.set_StatusDescription(字符串值)+4538824 System.Web.Mvc.HttpStatusCodeResult.ExecuteResult(ControllerContext 上下文)+109 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +88 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 个过滤器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)+775 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1 filters, ActionResult actionResult) +81 System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult) +188 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38 System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +26 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +40 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +68 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep 步骤)+195 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

Error Screenshot

最佳答案

错误消息指出在设置响应状态时发生异常:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value] System.Web.HttpResponse.set_StatusDescription(String value) +4538824

您的代码没有为 404 状态结果设置标准描述:

return new HttpStatusCodeResult(404,$"{ex.Message}|{ex.InnerException}");

将此更改为:

return new HttpStatusCodeResult(404,"Not Found");

描述可以省略,但是如果设置了,必须和状态码匹配。

关于c# - ArgumentOutOfRangeException , IIS express on 2012 server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627419/

相关文章:

asp.net-mvc - SignalR 请求抛出 "Hub could not be resolved."

asp.net-mvc - 无法将文件 .mdf 作为数据库附加

c# - 使用 C# 读取 json 值而不使用属性

c# - MVC 5 过滤器中的异步错误处理

c# - 在单声道中使用 USB 设备

c# - 如何使用 C# 使 Excel 单元格范围不可编辑

c# - 如何在 C# MVC 3 中返回空白 View

c# - 关于 List<T> 和 WCF 的说明

.net - Html.RenderPartial 慢

c# - 如何在ASP.net MVC中显示IIS服务器的自定义错误消息-5