示例#1
0
        public HttpResponseMessage AddUser([FromBody] User value, [FromUri] int userId)
        {
            var re      = Request;
            var headers = re.Headers;

            if (headers.Contains("userId"))
            {
                string token = headers.GetValues("userId").First();
            }
            if (ModelState.IsValid)
            {
                return((LogicUser.AddUser(value, userId)) ?
                       new HttpResponseMessage(HttpStatusCode.Created) :
                       new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new ObjectContent <String>("Can not add to DB", new JsonMediaTypeFormatter())
                });
            }
            ;
            List <string> ErrorList = new List <string>();

            //if the code reached this part - the user is not valid
            foreach (var item in ModelState.Values)
            {
                foreach (var err in item.Errors)
                {
                    ErrorList.Add(err.ErrorMessage);
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new ObjectContent <List <string> >(ErrorList, new JsonMediaTypeFormatter())
            });
        }