带有HttpResponseException问题的可查询装饰

时间:2014-07-10 12:21:27

标签: asp.net-web-api2

当我保留在Queryable中时,它会给出以下错误,如果我使用装饰器,我将如何处理抛出错误[可查询]:

查询无法应用于&System; Net.Net.Http.StringContent'类型的响应内容。响应内容必须是ObjectContent。

    [HttpGet]
    [Queryable]
    [ActionName("ById")]
    [ResponseType(typeof(Example))]
    public IQueryable<Example> Get(ODataQueryOptions opts, Guid actionuniqueid)
    {
        if (actionuniqueid.ToString() == "00000000-0000-0000-0000-000000000000")
        {
            var resp = new HttpResponseMessage(HttpStatusCode.NoContent)
            {
                Content =
                    new StringContent(
                    string.Format(
                        "No Example for ID: {0}", actionuniqueid)),
                ReasonPhrase = "Missing parameter"
            };
            throw new HttpResponseException(resp);
        }

1 个答案:

答案 0 :(得分:0)

尝试使用:

var resp = new HttpResponseMessage(HttpStatusCode.NoContent)
    {
        Content =
           new ObjectContent(typeof(string),
                string.Format("No Example for ID: {0}", actionuniqueid)),
                new JsonMediaTypeFormatter()),
        ReasonPhrase = "Missing parameter"
    };