使用asp.net的Web API发布数据

编程入门 行业动态 更新时间:2024-10-23 17:31:38
本文介绍了使用asp的Web API发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨每一个我是新来asp网站API.I要保存在数据库中的用户数据和我使用的邮递员传递数据。

Hi every one I am new to asp web API.I want to save user data in database and I am passing data using postman.

在我的API控制器我写了

In my API controller I wrote

public HttpResponseMessage PostCustomer([FromBody] NewUser userData) { userData = repository.Add(userData); var response = Request.CreateResponse<NewUser>(HttpStatusCode.Created, userData); string uri = Url.Link("DefaultApi", new { customerID = userData.ID }); response.Headers.Location = new Uri(uri); return response; }

新用户是班级为followes

NewUser is the class as followes

public class NewUser { [Key] public long ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public bool Status { get; set; } public DateTime? CreatedDate { get; set; } public DateTime? ModifiedDate { get; set; } }

在邮递员我形成数据我通过key和value它返回的我的消息如下:

In postman I form-data I passed key and value it return's me message as follows

{消息:请求实体的媒体类型的multipart / form-data的不支持该资源。,ExceptionMessage:没有MediaTypeFormatter可读取的内容类型的对象NEWUSER介质类型'multipart/form-data'.\",\"ExceptionType\":\"System.Net.Http.UnsupportedMediaTypeException\",\"StackTrace\":\"在System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型的IEnumerable 1格式化,IFormatterLogger formatterLogger,的CancellationToken的CancellationToken)\\ r \\ n在System.Net.Http.HttpContentExtensions.ReadAsAsync (HttpContent内容,类型类型的IEnumerable 1格式化,IFormatterLogger formatterLogger,的CancellationToken的CancellationToken)\\ r \\ n在System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HTT prequestMessage要求,类型类型,IEnumerable`1格式化,IFormatterLogger formatterLogger,的CancellationToken的CancellationToken)}

{"Message":"The request entity's media type 'multipart/form-data' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'NewUser' from content with media type 'multipart/form-data'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 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 me to save data from postman form-data.

推荐答案

试着遵循邮差的屏幕截图,这可能会帮助你。

Try to follow the screen shot from POSTMAN, this might help you.

身体需要设置为JSON。

Body needs to set to JSON.

更多推荐

使用asp.net的Web API发布数据

本文发布于:2023-11-16 09:38:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   net   asp   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!