【发布时间】:2014-09-16 12:57:11
【问题描述】:
使用 ASP.NET WebApi,当我发送 GET api/question?page=0&name=qwerty&other=params 并且 API 应该在分页信封内给出结果。
为此,我想将结果并将给定的page 查询字符串更改为其他值。
我尝试了下面的代码,但我对此有一种不好的感觉。
protected HttpResponseMessage CreateResponse(HttpStatusCode httpStatusCode, IEnumerable<Question> entityToEmbed)
// get QueryString and modify page property
var dic = new HttpRouteValueDictionary(Request.GetQueryNameValuePairs());
if (dic.ContainsKey("page"))
dic["page"] = (page + 1).ToString();
else
dic.Add("page", (page + 1).ToString());
var urlHelper = new UrlHelper(Request);
var nextLink= page > 0 ? urlHelper.Link("DefaultApi", dic) : null;
// put it in the envelope
var pageEnvelope = new PageEnvelope<Question>
{
NextPageLink = nextLink,
Results = entityToEmbed
};
HttpResponseMessage response = Request.CreateResponse<PageEnvelope<Question>>(httpStatusCode, pageEnvelope, this.Configuration.Formatters.JsonFormatter);
return response;
}
NextPageLink 给出了很多复杂的结果。:http://localhost/api/Question?Length=1&LongLength=1&Rank=1&SyncRoot=System.Collections.Generic.KeyValuePair%602%5BSystem.String%2CSystem.String%5D%5B%5D&IsReadOnly=False&IsFixedSize=True&IsSynchronized=False&page=1
我的问题是,
- 我使用
Dictionary方法处理的页面看起来很脏而且错误。有没有更好的方法来解决我的问题? - 我不知道为什么
urlHelper.Link(routeName, dic)会给出如此冗长的 ToString 结果。如何摆脱不可用的字典相关属性?
【问题讨论】:
标签: c# asp.net-web-api query-string