使用asp.net Web API发布数据

时间:2016-04-17 12:14:12

标签: asp.net-mvc asp.net-web-api

大家好我是asp.net web API的新手。我想在数据库中保存用户数据,并且我使用邮递员传递数据。

在我的API控制器中,我写了

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;
}

NewUser是以下类

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; }
    }

在邮递员我表格 - 数据我传递密钥并重视它返回我的消息如下

  

{&#34;消息&#34;:&#34;请求实体的媒体类型&#39; multipart / form-data&#39;此资源不支持。&#34;,&#34; ExceptionMessage&#34;:&#34;没有MediaTypeFormatter可用于读取类型&#39; NewUser&#39;来自媒体类型的内容&#39; multipart / form-data&#39;。&#34;,&#34; ExceptionType&#34;:&#34;&n; 34; System.Net.Http.UnsupportedMediaTypeException&#34;,&#34 ;栈跟踪&#34;:&#34;在System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型,IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable 1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\ r \ n在System.Web.Http.ModelBinding.FormatterParameterBinding。 ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)&#34;}

请帮我保存邮递员表格数据。

2 个答案:

答案 0 :(得分:5)

尝试按照POSTMAN的屏幕截图,这可能对您有帮助。

正文需要设置为JSON。

enter image description here

答案 1 :(得分:0)

如果内容类型设置为application / json,则您的正文应该以json格式构建

相关问题