【发布时间】: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