How to return a 200 HTTP Status Code from ASP.NET MVC 3 controller

前端 未结 5 1505
刺人心
刺人心 2020-12-12 11:48

I am writing an application that is accepting POST data from a third party service.

When this data is POSTed I must return a 200 HTTP Status Code.

How can I

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 12:24

        [HttpPost]
        public JsonResult ContactAdd(ContactViewModel contactViewModel)
        {
            if (ModelState.IsValid)
            {
                var job = new Job { Contact = new Contact() };
    
                Mapper.Map(contactViewModel, job);
                Mapper.Map(contactViewModel, job.Contact);
    
                _db.Jobs.Add(job);
    
                _db.SaveChanges();
    
                //you do not even need this line of code,200 is the default for ASP.NET MVC as long as no exceptions were thrown
                //Response.StatusCode = (int)HttpStatusCode.OK;
    
                return Json(new { jobId = job.JobId });
            }
            else
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(new { jobId = -1 });
            }
        }
    

提交回复
热议问题