How to return a list of objects as an IHttpActionResult?

前端 未结 4 1497
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 09:19

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

This is my controller method for the GET request. I want to return all the

4条回答
  •  旧时难觅i
    2021-02-08 09:40

    Just simply return it like this, you need to use one of the nice methods that ApiController now supplies.

    This will return a status code of 200 along with your questions collection.

    [ResponseType(typeof(List))]
    public async Task 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 this.Ok(questions);
    }
    

提交回复
热议问题