我的服务返回200状态代码,但是我的MVC控制器IsSuccessStatusCode返回FALSE

时间:2018-07-27 20:20:29

标签: c# asp.net-mvc web-services asp.net-web-api

我有一个用Web API在.NET中开发的Web服务,该服务具有执行新用户注册的控制器。记录完成后,它将返回HttpActionResult为200的HttpStatusCode(确定),但是我在MVC中的项目控制器将其解释为500(内部服务器错误)。我已经这样做了几个小时,但找不到原因。这不是在控制器后面运行的任务中的错误,因为try / catch会抛出该错误,但不会发生。这是服务控制器给我这些问题的唯一方法。知道为什么会发生吗?

这是我的Web API控制器的方法(发送者):

public IHttpActionResult RegisterAttendee(ParticipantDTO participantDto)
{
    if (!ModelState.IsValid)
        return BadRequest();
    int result = _context.RegisterParticipant(participantDto);
    return Content(HttpStatusCode.OK, result);
}

这是我的MVC控制器的方法(接收方):

    [HttpPost]
    [AllowAnonymous]
    [ValidateJsonAntiForgeryToken]
    public async Task<ActionResult> RegisterParticipant(RegisterIndexBaseVm attendee)
    {
        if (attendee == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        if (ModelState.IsValid)
        {
            attendee.EventId = Int32.Parse(Session["id"].ToString());
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUrl);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage Res = await client.PostAsync("Register/RegisterAttendee", new StringContent(JsonConvert.SerializeObject(Mapper.ToParticipant(attendee)), Encoding.UTF8, "application/json"));
                if (Res.IsSuccessStatusCode)
                {
                    var EmpResponse = Res.Content.ReadAsStringAsync().Result;
                    var result = JsonConvert.DeserializeObject<int>(EmpResponse);
                    if (result == 0)
                        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                    else
                        return Json(new { success = false }, JsonRequestBehavior.AllowGet);
                }
            }
        }
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

感谢支持

0 个答案:

没有答案