如何将 return 对象列表作为 IHttpActionResult?
How to return a list of objects as an IHttpActionResult?
我是 ASP.NET webapi 的新手,我找不到 return 按 id 查询的对象列表的方法。
这是我的 GET 请求控制器方法。
我想 return 所有具有指定 questionnaireId 通过 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;
}
这是我的问题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; }
}
但是在 return questions
它显示编译器错误:
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?)
我想得到一个问题列表 returned in JSON queried on questionnaireId, which is passed via a url i.e. api/questions/2 ==> 给了我用 questionnaireId = 2 返回所有问题。
我认为您正在寻找类似于以下的代码:
public IEnumerable<Question> Get(int id)
{
//Create the list that you need to return here
// I am creating a new list and adding an object below just for
// explaining the idea.
var questions = new List<Question>();
questions.Add(new Question());
return questions;
}
首先不要直接使用实体来提供数据。
为您的实体创建 DTO:
public class QuestionDto
{
public int id {get; set;}
//put here getter and setter for all other Question attributes you want to have
public QuestionDto(Question question){
this.id = question.id;
... and so on
}
}
那么您的 GET 方法可能如下所示:
// GET: api/Questions/5
public List<QuestionDto> GetQuestion(int questionnaireId)
{
IEnumerable<QuestionDto> questions = from q in db.Questions
where q.QuestionnaireId == questionnaireId
select new QuestionDto(q);
return questions.toList();
}
我还建议使用 JSON 进行数据传输,因为使用 Javascript 非常容易。
您正在使用 [ResponseType]
属性,但这仅用于生成文档,请参阅 MSDN: ResponseTypeAttribute Class:
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.
您可以更改 return 类型(并删除该属性,因为不再需要它,因为 return 类型文档将从实际签名生成):
public IEnumerable<Question> GetQuestion(int questionnaireId)
或者,如果您希望它是异步的:
public async Task<IEnumerable<Question>> GetQuestion(int questionnaireId)
或者将结果包装在 IHttpActionResult
中,方法 Request.CreateResponse<T>()
所做的是:
return Request.CreateResponse<IEnumerable<Question>>(HttpStatusCode.OK, questions);
如果您调用 ApiController.Ok()
方法,后者会为您完成:
return Ok(questions);
只是简单地 return 就像这样,您需要使用 ApiController 现在提供的一种不错的方法。
这将 return 状态代码 200 以及您的问题集合。
[ResponseType(typeof(List<Question>))]
public async 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 this.Ok(questions);
}
我是 ASP.NET webapi 的新手,我找不到 return 按 id 查询的对象列表的方法。
这是我的 GET 请求控制器方法。 我想 return 所有具有指定 questionnaireId 通过 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;
}
这是我的问题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; }
}
但是在 return questions
它显示编译器错误:
Cannot implicitly convert type
System.Linq.IQueryable<finah_backend.Models.Question>
toSystem.Web.Http.IHttpActionResult
. An explicit conversion exists (are you missing a cast?)
我想得到一个问题列表 returned in JSON queried on questionnaireId, which is passed via a url i.e. api/questions/2 ==> 给了我用 questionnaireId = 2 返回所有问题。
我认为您正在寻找类似于以下的代码:
public IEnumerable<Question> Get(int id)
{
//Create the list that you need to return here
// I am creating a new list and adding an object below just for
// explaining the idea.
var questions = new List<Question>();
questions.Add(new Question());
return questions;
}
首先不要直接使用实体来提供数据。 为您的实体创建 DTO:
public class QuestionDto
{
public int id {get; set;}
//put here getter and setter for all other Question attributes you want to have
public QuestionDto(Question question){
this.id = question.id;
... and so on
}
}
那么您的 GET 方法可能如下所示:
// GET: api/Questions/5
public List<QuestionDto> GetQuestion(int questionnaireId)
{
IEnumerable<QuestionDto> questions = from q in db.Questions
where q.QuestionnaireId == questionnaireId
select new QuestionDto(q);
return questions.toList();
}
我还建议使用 JSON 进行数据传输,因为使用 Javascript 非常容易。
您正在使用 [ResponseType]
属性,但这仅用于生成文档,请参阅 MSDN: ResponseTypeAttribute Class:
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.
您可以更改 return 类型(并删除该属性,因为不再需要它,因为 return 类型文档将从实际签名生成):
public IEnumerable<Question> GetQuestion(int questionnaireId)
或者,如果您希望它是异步的:
public async Task<IEnumerable<Question>> GetQuestion(int questionnaireId)
或者将结果包装在 IHttpActionResult
中,方法 Request.CreateResponse<T>()
所做的是:
return Request.CreateResponse<IEnumerable<Question>>(HttpStatusCode.OK, questions);
如果您调用 ApiController.Ok()
方法,后者会为您完成:
return Ok(questions);
只是简单地 return 就像这样,您需要使用 ApiController 现在提供的一种不错的方法。
这将 return 状态代码 200 以及您的问题集合。
[ResponseType(typeof(List<Question>))]
public async 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 this.Ok(questions);
}