コード例 #1
0
        public HttpResponseMessage Put(String email, [FromBody] SUPERADMIN SUPERADMIN)
        {
            try
            {
                using (knowhauEntities entities = new knowhauEntities())
                {
                    var entity = entities.SUPERADMINs.FirstOrDefault(e => e.email == email);
                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "SUPERADMIN with email " + email.ToString() + " not found to update"));
                    }
                    else
                    {
                        entity.email    = SUPERADMIN.email;
                        entity.name     = SUPERADMIN.name;
                        entity.password = SUPERADMIN.password;
                        entity.username = SUPERADMIN.username;
                        entities.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
コード例 #2
0
        public HttpResponseMessage Post([FromBody] SUPERADMIN superadmin)
        {
            try
            {
                using (knowhauEntities entities = new knowhauEntities())
                {
                    entities.SUPERADMINs.Add(superadmin);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, superadmin);
                    message.Headers.Location = new Uri(Request.RequestUri +
                                                       superadmin.name.ToString());

                    return(message);
                }
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }