【问题标题】:Unable to cast ObjectContent to HttpContent无法将 ObjectContent 转换为 HttpContent
【发布时间】:2017-09-28 12:18:55
【问题描述】:

以下代码在我从笔记本电脑上的 Visual Studio 运行时有效。但是当部署到 Windows 2016 服务器上的 IIS 时,我收到 500 错误和异常(.NET Framework 4.7):

ExceptionMessage:“从‘System.Net.Http.HttpResponseMessage’上的‘内容’获取值时出错。”
异常类型:“Newtonsoft.Json.JsonSerializationException”

内部异常
ExceptionMessage:“无法将'System.Net.Http.ObjectContent'类型的对象转换为'System.Net.Http.HttpContent'类型。”
异常类型:“System.InvalidCastException”
public HttpResponseMessage Get()
{
    // Entities is the Entity Framework context
    // Sessions is a proprietary table and has nothing to do with ASP.NET sessions
    var content = new Entities().Sessions
        .AsEnumerable()
        .Select(t =>
        {
            return new
            {
                t.Id,
                t.SessionId,
                t.StartTime,
                t.EndTime
            };
        });

    return new HttpResponseMessage
    {
        StatusCode = HttpStatusCode.OK,
        Content    = new ObjectContent(typeof(IEnumerable), content, new JsonMediaTypeFormatter())
    };
}

【问题讨论】:

    标签: c# asp.net-web-api2


    【解决方案1】:

    显然在 IIS (Windows Server 2016) 上使用 .NET 4.7 并不是一个好主意。当我将我的项目降级到 4.6 时,事情开始起作用了......

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 2015-09-04
      • 2016-05-26
      • 2015-08-11
      • 1970-01-01
      • 2012-04-08
      相关资源
      最近更新 更多