我的asp.net wep api不接受json,为什么? [英] My asp.net wep api isn't accepting json, why?

查看:59
本文介绍了我的asp.net wep api不接受json,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我正在使用mvc制作一个asp.net web api。我在默认控制器中都有GET / POST / PUT / DELETE。但我现在正在测试的是帖子(得到的工作正常),我正在使用chrome的邮递员扩展来测试这个以及发生的事情是邮递员因为媒体类型给我一个错误任何想法如何解决这个?



POST控制器:



 // POST:api / Events 
[HttpPost]
[ResponseType(typeof(Event))]
public async Task< IHttpActionResult> PostBook(事件事件)
{
if(!ModelState.IsValid)
{
返回BadRequest(ModelState);
}

db.Events.Add(events);
等待db.SaveChangesAsync();

//新代码:
//加载作者姓名
db.Entry(events).Reference(e => e.Name).Load();

var dto = new EventDto()
{
Name = events.Name,
Project = events.Project,
Objectives = events.Objectives,
City = events.City,
Country = events.Country,
EventStart = events.EventStart,
Departure = events.Departure,
Arrival = events.Arrival,
注册= events.Registration,
NationalTransportation = events.NationalTransportation,
住宿= events.Accommodation,
AcNumberNights = events.AcNumberNights,
AcPreferHotel = events.AcPreferHotel,
AcPreferHotelUrl = events.AcPreferHotelUrl,
Flight = events.Flight,
FlDeparture = events.FlDeparture,
FlDepartPrefer = events.FlDepartPrefer,
FlDepartPreferUrl = events.FlDepartPreferUrl,
FlReturn = events.FlReturn,
FlRetPrefer = events.FlRetPrefer,
FlRetPreferUrl = events.FlRetPreferUrl,
Notes = events.Notes,
Files = events.Files,
Status = events.Status
};
返回CreatedAtRoute(DefaultApi,new {id = events.Id},dto);
}







JSON我在邮递员中插入:



 {
名称:psilva,
项目:a,
目标: s,
城市:a,
国家:s,
EventStart:2012年,
离境:2012年,
抵达:2012年,
注册:a,
NationalTransportation:a,
住宿:a,
AcNumberNights :1,
AcPreferHotel:a,
AcPreferHotelUrl:a,
航班:a,
FlDeparture:2012,
FlDepartPrefer:a,
FlDepartPreferUrl:a,
FlReturn:2012,
FlRetPrefer:a,
FlRetPreferUrl:a,
注释:a,
文件:a,
状态:a
}




来自邮递员的
错误:



 {
消息:此资源不支持请求实体的媒体类型'text / plain'。,
ExceptionMessage:没有MediaTypeFormatter可用于读取对象从媒体类型为'text / plain'的内容输入'Event'。,
ExceptionType:System.Net.Http.UnsupportedMediaTypeException,
StackTrace:at System.Net.Http .HttpContentExtensions.ReadAsAsync [T](HttpContent内容,Type类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\\\ System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent内容,类型类型,IEnumerable`在System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)中有1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\\\\ n
}





请帮助

解决方案

你需要手动添加' application / json'标题到你的帖子请求 - 我不记得细节,你加上th是你的http客户端iirc


hey guys, I'm making an asp.net web api using mvc. I have the GET/POST/PUT/DELETE all in the default controller. But what I'm testing now is the post(get is working fine), I'm using the postman extension of chrome to test this and what is happening is that postman is giving me an error due to media types any ideas how to fix this?

POST controller:

// POST: api/Events
        [HttpPost]
        [ResponseType(typeof(Event))]
        public async Task<IHttpActionResult> PostBook(Event events)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Events.Add(events);
            await db.SaveChangesAsync();

            // New code:
            // Load author name
            db.Entry(events).Reference(e => e.Name).Load();

            var dto = new EventDto()
            {
                Name = events.Name,
                Project = events.Project,
                Objectives = events.Objectives,
                City = events.City,
                Country = events.Country,
                EventStart = events.EventStart,
                Departure = events.Departure,
                Arrival = events.Arrival,
                Registration = events.Registration,
                NationalTransportation = events.NationalTransportation,
                Accommodation = events.Accommodation,
                AcNumberNights = events.AcNumberNights,
                AcPreferHotel = events.AcPreferHotel,
                AcPreferHotelUrl = events.AcPreferHotelUrl,
                Flight = events.Flight,
                FlDeparture = events.FlDeparture,
                FlDepartPrefer = events.FlDepartPrefer,
                FlDepartPreferUrl = events.FlDepartPreferUrl,
                FlReturn = events.FlReturn,
                FlRetPrefer = events.FlRetPrefer,
                FlRetPreferUrl = events.FlRetPreferUrl,
                Notes = events.Notes,
                Files = events.Files,
                Status = events.Status
            };
            return CreatedAtRoute("DefaultApi", new { id = events.Id }, dto);
        } 




JSON I'm inserting in postman:

{
"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"    
}



error from 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)"
}



Please help

解决方案

you need to manually add an 'application/json' header to your post request - I cant remember the details, you add this to your http client iirc


这篇关于我的asp.net wep api不接受json,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆