使用asp.net的Web API发布数据 [英] post data using asp.net Web API
问题描述
嗨每一个我是新来asp.net网站API.I要保存在数据库中的用户数据和我使用的邮递员传递数据。
Hi every one I am new to asp.net 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, 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 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发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!