Json Post请求asp.net web api无效 [英] Json Post request to asp.net web api not working
本文介绍了Json Post请求asp.net web api无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,我有一个带有这个控制器的as.net web api应用程序:
Hey guys, I have an as.net web api application with this controller:
// GET: api/Events/5
[ResponseType(typeof(EventDto))]
public async Task<IHttpActionResult> GetEvent(int id)
{
var events = await db.Events.Include(e => e.Name).Select(e =>
new EventDto()
{
Id = e.Id,
Name = e.Name,
Project = e.Project,
Objectives = e.Objectives,
City = e.City,
Country = e.Country,
EventStart = e.EventStart,
Departure = e.Departure,
Arrival = e.Arrival,
Registration = e.Registration,
NationalTransportation = e.NationalTransportation,
Accommodation = e.Accommodation,
AcNumberNights = e.AcNumberNights,
AcPreferHotel = e.AcPreferHotel,
AcPreferHotelUrl = e.AcPreferHotelUrl,
Flight = e.Flight,
FlDeparture = e.FlDeparture,
FlDepartPrefer = e.FlDepartPrefer,
FlDepartPreferUrl = e.FlDepartPreferUrl,
FlReturn = e.FlReturn,
FlRetPrefer = e.FlRetPrefer,
FlRetPreferUrl = e.FlRetPreferUrl,
Notes = e.Notes,
Files = e.Files,
Status = e.Status
}).SingleOrDefaultAsync(e => e.Id == id);
if (events == null)
{
return NotFound();
}
return Ok(events);
}
我正在使用postman chrome扩展程序用这个json测试它:
and I'm using postman chrome extension to test it with this json:
{
"Name": "psilva",
"Project": "a",
"Objectives": "s",
"City": "a",
"Country": "s",
"EventStart": 2012,
"Departure": 2012,
"Arrival": 2012,
"Registration": "a",
"NationalTransportation": "a",
"Accommodation" : "a",
"AcNumberNights": 1,
"AcPreferHotel": "a",
"AcPreferHotelUrl": "a",
"Flight": "a",
"FlDeparture": 2012,
"FlDepartPrefer": "a",
"FlDepartPreferUrl": "a",
"FlReturn": 2012,
"FlRetPrefer": "a",
"FlRetPreferUrl": "a",
"Notes": "a",
"Files": "a",
"Status": "a"
}
我在邮递员身上得到这个:
and I'm getting this on the postman:
{
"Message": "The request entity's media type 'text/plain' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Event' from content with media type 'text/plain'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
任何想法为什么?
any idea why?
推荐答案
这是因为您需要将媒体类型更改为
That is because you need to change the media-type to,
Content-Type: application/json
,Web API接受并处理它。不支持 text / plain
,这是异常告诉你的。
for the Web API to accept and work on it. text/plain
is not supported, that is what exception is telling you.
这篇关于Json Post请求asp.net web api无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文