无法将 ObjectContent 转换为 HttpContent
Unable to cast ObjectContent to HttpContent
下面的代码 当我从 Visual Studio 在我的笔记本电脑 上 运行 它时有效。但是当部署到 Windows 2016 服务器上的 IIS 时,我收到 500 错误和异常(.NET Framework 4.7):
ExceptionMessage: "Error getting value from 'Content' on 'System.Net.Http.HttpResponseMessage'."
ExceptionType : "Newtonsoft.Json.JsonSerializationException"
InnerException
ExceptionMessage: "Unable to cast object of type 'System.Net.Http.ObjectContent' to type 'System.Net.Http.HttpContent'."
ExceptionType : "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())
};
}
显然在 IIS (Windows Server 2016) 上使用 .NET 4.7 不是一个好主意。当我将我的项目降级到 4.6 时,事情开始起作用了...
下面的代码 当我从 Visual Studio 在我的笔记本电脑 上 运行 它时有效。但是当部署到 Windows 2016 服务器上的 IIS 时,我收到 500 错误和异常(.NET Framework 4.7):
ExceptionMessage: "Error getting value from 'Content' on 'System.Net.Http.HttpResponseMessage'." ExceptionType : "Newtonsoft.Json.JsonSerializationException" InnerException ExceptionMessage: "Unable to cast object of type 'System.Net.Http.ObjectContent' to type 'System.Net.Http.HttpContent'." ExceptionType : "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())
};
}
显然在 IIS (Windows Server 2016) 上使用 .NET 4.7 不是一个好主意。当我将我的项目降级到 4.6 时,事情开始起作用了...