返回IHttpActionResult而不仅仅是自定义json对象

时间:2017-08-31 02:04:47

标签: c# json asp.net-web-api2

    [HttpGet]
    [Route("searchparts")]
    public IHttpActionResult SearchResults([FromUri] PartsQuery query)
    {
        try
        {
            //Model Validation is done using ValidateActionFilter
            SearchResults data = DataHelpers.ConvertSearchResults(_search.RelatedPartNumber(query.PartFamily, query.AdditionalKeywords, query.MaxCount));
            if (data == null)
            {
                return BadRequest("An error occured while processing the request. Null values were returned.");
            }
            else
            {
                return Ok<SearchResults>(data);
            }
        }
        catch (Exception e)
        {
            return BadRequest(e.Message);
        }
    }

所以这是示例Web API 2代码。我想返回包含Http Status的IHttpActionResult,但我得到的只是SearchResults的json响应。如何更改此代码以获取响应中包含的Http信息。我查找的所有示例基本上都说返回Ok(数据)会起作用。我在这里缺少什么?

0 个答案:

没有答案