调用自己的Action方法返回IHttpActionResult

时间:2017-03-31 00:30:39

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

似乎我的问题可能不清楚。这是我想要做的。

我正在编写一个web api,可以调用另一个web api。在我的API内部,我想从MarkAllTask​​sDone方法调用UpdateAssignmentStatus方法。我不知道如何处理IHttpActionResult的返回类型,这意味着如何处理它。如何获取方法中返回的信息。在UpdateAssignmentStatus方法中,它返回带有消息的BadRequest,带有消息的InternalServerError或带消息的Ok。我如何获得这些数据?对不起,我之前应该提供这些信息。

在这里添加更多内容。当我调试时,我可以看到actionResultUAS对象是我在UpdateAssignmentStatus方法中声明的返回类型之一。 BadRequest返回System.Web.Http.Results.BadRequestErrorMessageResult,Ok返回System.Web.Http.Results.OkNegotiatedContentResult,而InternalServerError返回System.Web.Http.Results.ExceptionResult。我假设我需要转换为每种类型。有没有一种好方法可以做到这一点,也许是一种通用的方式。

    [HttpGet]
    public IHttpActionResult MarkAllTasksDone(string projectID)
    {
        try
        {
            var assignments = GetAssignments("ASSGN/search?projectID=" + projectID + "&fields=taskID&fields=status" + "&apiKey=" + ApiKey);

            foreach (var assignment in assignments.Where(a => a.AssignmentStatus != "DN"))
            {
                var actionResultUAS = UpdateAssignmentStatus(assignment.AssignmentID, "DONE");
                //****how to get the ok or failure code and the returned data from the actionResultUAS object

                var responseMessageUT = UpdateTaskPercentComplete(assignment.TaskID, 100);
                }
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }

        return Ok();

    }


[HttpGet]
public IHttpActionResult UpdateAssignmentStatus(string assignmentID, string assignmentStatus)
{
    HttpResponseMessage httpResponse = new HttpResponseMessage();
    try
    {
        var convertedStatus = ConvertAssignmentStatus(assignmentStatus);
        httpResponse = Put("ASSGN?ID=" + assignmentID + "&status=" + convertedStatus + "&apiKey=" + ApiKey);
    }
    catch (ApplicationException ae)
    {
        return BadRequest(ae.Message);

    }
    catch (Exception ex)
    {
        return InternalServerError(ex);
    }
    HttpResponseData httpResponseData = new HttpResponseData();
    httpResponseData.IsSuccessful = httpResponse.IsSuccessStatusCode;
    httpResponseData.Content = httpResponse.Content.ReadAsStringAsync().Result;
    httpResponseData.StatusCode = httpResponse.StatusCode.ToString();

    return Ok(httpResponseData);
}

2 个答案:

答案 0 :(得分:0)

您可以使用:

let attributes = try FileManager.default.attributesOfItem(atPath: url.relativePath)

编辑:当您从结果中请求对象时,您可以检查状态代码并读取对象中的响应。

  return Content(httpResponse.StatusCode, httpResponse);

这将返回带有响应的模型。

答案 1 :(得分:-1)

您需要在Api方法中返回状态代码和类似信息,

return Content(HttpStatusCode.BadRequest, myData);

OR

return Content(HttpStatusCode.OK, myData);

然后,

try
{
    var convertedStatus = ConvertAssignmentStatus(assignmentStatus);
    //httpResponse = Put("ASSGN?ID=" + assignmentID + "&status=" + //convertedStatus + "&apiKey=" + ApiKey);

    var http = new HttpClient { BaseAddress =  
                                new Uri("http://localhost:11111/") };
    //I'm passing the value as query string. See the bellow line
    string apiUrl = "api/ControllerName/ASSGN?ID=" + assignmentID + 
                    "&status=" + convertedStatus + "&apiKey=" + ApiKey";


    var _response = http.PutAsJsonAsync(http.BaseAddress + apiUrl, "").Result;

    var returnStatus = _response.StatusCode;
}