如何将对象列表作为IHttpActionResult返回? [英] How to return a list of objects as an IHttpActionResult?

查看:59
本文介绍了如何将对象列表作为IHttpActionResult返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET webapi的新手,我无法找到一种方法来返回按ID查询的对象列表.

I'm new to ASP.NET webapi and I can't find a way to return a list of objects queried by id.

这是我对GET请求的控制器方法.我想返回所有通过网址传递的具有指定问卷调查表ID的问题.

This is my controller method for the GET request. I want to return all the questions which have a specified questionnaireId passed via url.

我尝试过:

// GET: api/Questions/5
[ResponseType(typeof(List<Question>))]
public Task<IHttpActionResult> GetQuestion(int questionnaireId)
{
    var questions = from q in db.Questions
    where q.QuestionnaireId == questionnaireId
    select new Question()
    {
            Id = q.Id,
            ImageLink = q.ImageLink,
            QuestionnaireId = q.QuestionnaireId,
            Text = q.Text
    };
    return questions;
}

这是我的Question类:

This is my Question class:

public class Question
    {
        public int Id { get; set; }

        [ForeignKey("Questionnaire")]
        public int QuestionnaireId { get; set; }

        public string Text { get; set; }
        public string ImageLink { get; set; }

        public virtual Questionnaire Questionnaire { get; set; }
    }

但是在返回问题上,它显示了编译器错误:

But on return questions it shows the compiler error:

无法将类型 System.Linq.IQueryable< finah_backend.Models.Question> 隐式转换为 System.Web.Http.IHttpActionResult .存在显式转换(您是否缺少演员表?)

Cannot implicitly convert type System.Linq.IQueryable<finah_backend.Models.Question> to System.Web.Http.IHttpActionResult. An explicit conversion exists (are you missing a cast?)

我想获取在问卷调查表ID上以JSON查询的问题列表,该列表通过url传递,即api/questions/2 ==>给出了所有问卷调查表ID = 2的问题.

I want to get a list of questions returned in JSON queried on questionnaireId, which is passed via a url i.e. api/questions/2 ==> gives me back all the questions with questionnaireId = 2.

推荐答案

您正在使用 [ResponseType] 属性,但这仅用于生成文档,请参见

You're using the [ResponseType] attribute, but that's only for generating documentation, see MSDN: ResponseTypeAttribute Class:

当声明的返回类型为HttpResponseMessage或IHttpActionResult时,使用此选项指定操作返回的实体类型.生成ApiDescription时,ApiExplorer将读取ResponseType.

Use this to specify the entity type returned by an action when the declared return type is HttpResponseMessage or IHttpActionResult. The ResponseType will be read by ApiExplorer when generating ApiDescription.

您可以更改返回类型(并删除属性,因为不再需要该属性,因为将从实际签名生成返回类型文档):

You can either change your return type (and remove the attribute, as it isn't required anymore as the return type documentation will be generated from the actual signature):

public IEnumerable<Question> GetQuestion(int questionnaireId)

或者,如果您希望它是异步的:

Or, if you want it to be async:

public async Task<IEnumerable<Question>> GetQuestion(int questionnaireId)

或将结果包装在 IHttpActionResult 中,该方法由 Request.CreateResponse< T>()方法执行:

Or wrap the result in an IHttpActionResult, which the method Request.CreateResponse<T>() does:

 return Request.CreateResponse<IEnumerable<Question>>(HttpStatusCode.OK, questions);

如果您调用 ApiController.Ok()方法,则后者将为您完成:

The latter is done for you if you call the ApiController.Ok() method:

return Ok(questions);

这篇关于如何将对象列表作为IHttpActionResult返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆