Asp.net mvc 使用asp.net Web API发布数据

Asp.net mvc 使用asp.net Web API发布数据,asp.net-mvc,asp.net-web-api,Asp.net Mvc,Asp.net Web Api,大家好,我是asp.NETWebAPI新手。我想在数据库中保存用户数据,我正在使用postman传递数据 在我的API控制器中,我编写了 public HttpResponseMessage PostCustomer([FromBody] NewUser userData) { userData = repository.Add(userData); var response = Request.CreateResponse<NewUser>(HttpStatusCo

大家好,我是asp.NETWebAPI新手。我想在数据库中保存用户数据,我正在使用postman传递数据

在我的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;
}
在postman I表单数据中,我传递了键和值,它返回的消息如下

{“Message”:“此资源不支持请求实体的媒体类型‘multipart/form data’”,“ExceptionMessage”:“没有MediaTypeFormatter可用于从媒体类型为‘multipart/form data’的内容中读取‘NewUser’类型的对象。”,“ExceptionType”:“System.Net.Http.UnsupportedMediaTypeException”,“StackTrace”::“位于System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容,类型,IEnumerable
1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)\r\n位于System.Net.Http.HttpContentExtensions.Readasync(HttpContent内容,类型类型,IEnumerable
1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)\r\n位于System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)“}


请帮助我保存来自邮递员表单数据的数据。

尝试按照邮递员的屏幕截图进行操作,这可能会对您有所帮助

Body需要设置为JSON


试着按照邮递员的屏幕截图进行操作,这可能会对您有所帮助

Body需要设置为JSON


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

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

在从邮递员处检查时您使用的内容类型是什么?我使用application/json;charset=utf-8检查时您使用的内容类型是什么从POSTMAN?我在Web API中使用application/json;charset=utf-8,这不是强制性的。但如果服务器端逻辑需要其中一些,则应确保使用适当的验证属性来修饰它们,以确保它们存在。POSTMAN充当客户端与Web API交互。标头的内容类型,即发送json请求的主体t、 此JSON将被序列化为要进一步使用的对象。感谢这项工作。我的错误是我使用的是表单数据。如何使用表单数据?请接受答案:)…我通常不使用表单数据,因为Web API应由不同的客户端使用。请在Web API方面通过此链接,这不是强制性的。但是,如果您的服务器端逻辑需要其中一些,您应该确保使用适当的验证属性来装饰它们,以确保它们存在。邮递员充当客户端与W进行交互eb API。标头的内容类型,发送JSON请求的主体。此JSON将被序列化为要进一步使用的对象。感谢这项工作。我错了,我使用的是表单数据。如何使用表单数据?请接受答案:)…我通常不使用表单数据,因为Web API应由不同的客户端使用。请通过此链接
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; }
    }