我有一个使用 ServiceStack 创建的服务。最近我更新了 ServiceStack 库,现在我得到的是 JSV 响应而不是 JSON 响应。
该请求类似于:
POST http://localhost/api/rest/poll/create?format=json&PollFormat=1 HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 160
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: 1
Referer: http://localhost
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie:
Question=this+is+a+test&Answers=yes%2Cno&
响应看起来像:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ServiceStack/3.956 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 12 Aug 2013 21:20:33 GMT
Content-Length: 437
{Id:1,Question:this is a test,Answers:[{Id:1,Text:yes,Votes:0},{Id:2,Text:no,Votes:0}],IsOpen:1,TotalVotes:0}}
请注意,我已将响应中的 JSV 剪裁了一些,以使其更易于阅读,因此,该示例的 Content-Length 将不正确。
据我了解,ServiceStack 的默认 ContentType 应该是JSON
那么为什么我要返回 JSV 的 ContentType 为 application/json 呢?
编辑:
这是我的请求 dto 的样子:
[Route("/poll/create", Verbs = "POST")]
public class PollRequest : IReturn<Object>
{
public string Question { get; set; }
public string Answers { get; set; }
public int? PollFormat { get; set; }
}
这是我的服务的样子:
public class PollService : Service
{
public object Post(PollRequest request)
{
//
// do work required to create new poll
//
Poll p = new Poll();
if(request.PollFormat.HasValue)
{
return JsonSerializer.DeserializeFromString<object>(p.JSON);
}
else
{
return PostConvertor.ConvertTo(p);
}
}
}
这是我的投票响应的样子:
public class Poll
{
public int Id { get; set; }
public string Question { get; set; }
public Collection<Answer> Answers { get; set; }
public int IsOpen { get; set; }
public int TotalVotes { get; set; }
public class Answer
{
public int Id { get; set; }
public string Text { get; set; }
public int Votes { get; set; }
}
}