我正在使用ServiceStack创建RESTful服务,该服务应该使用带有multipart / form-data内容的POST。内容采用JSON格式,但是当我发送POST时,对象没有正确反序列化(所有属性都是null / default值)。如果我尝试将该对象作为常规POST(没有multipart / form-data)发送,则反序列化就好了。
我在ServiceStack代码中试图弄清楚发生了什么,这是我目前的理解:
我确认JsvReader.ParseFnCache中有很多类型,而JsonReader.ParseFnCache是空的。此外,如果我更改我的请求以删除所有引号(即将其从JSON转换为JSV格式),它会正确反序列化。一个奇怪的事情是我的对象的一个属性是一个Dictionary,并且正确地反序列化,即使它是JSON格式的;我假设这只是一个幸运的巧合(?!?)。
我理解这里发生的事情是否正确?这是ServiceStack中的已知限制吗?错误?除了将我的对象放在一个文件中并手动调用JsonSerializer.DeserializeFromStream()之外,还有解决方法吗?
谢谢!
JPS
此外,只是因为它很有用,这里是相关的请求和数据对象:
POST /api/Task HTTP/1.1
Accept: application/json
Content-Type: multipart/form-data; boundary=Boundary_1_1161035867_1375890821794
MIME-Version: 1.0
Host: localhost:12345
Content-Length: 385
--Boundary_1_1161035867_1375890821794
Content-Type: application/json
Content-Disposition: form-data; name="MyMap"
{"myfile.dat":"ImportantFile"}
--Boundary_1_1161035867_1375890821794
Content-Disposition: form-data; name="MyThing"
Content-Type: application/json
{"Id":123,"Name":"myteststring"}
--Boundary_1_1161035867_1375890821794
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="myfile.dat"
mydatagoeshere...
--Boundary_1_1161035867_1375890821794--
public class TestObj
{
public long Id { get; set; }
public string Name { get; set; }
}
[Route("/Task", "POST")]
public class TaskRequest : AuthenticatedRequest, IReturn<TaskResponse>
{
public TestObj MyThing { get; set; }
public Dictionary<string, string> MyMap { get; set; }
}
答案 0 :(得分:1)
您是否尝试使用'ApiMember'属性设置请求对象的属性?特别是'ParameterType'属性。
/// <summary>
/// Create and upload a new video.
/// </summary>
[Api("Create and upload a new video.")]
[Route("/videos", "POST", Summary = @"Create and upload a new video.",
Notes = "Video file / attachment must be uploaded using POST and 'multipart/form-data' encoding.")]
public class CreateVideo : OperationBase<IVideo>
{
/// <summary>
/// Gets or sets the video title.
/// </summary>
[ApiMember(Name = "Title",
AllowMultiple = false,
DataType = "string",
Description = "Video title. Required, between 8 and 128 characters.",
IsRequired = true,
ParameterType = "form")]
public string Title { get; set; }
/// <summary>
/// Gets or sets the video description.
/// </summary>
[ApiMember(Name = "Description",
AllowMultiple = false,
DataType = "string",
Description = "Video description.",
ParameterType = "form")]
public string Description { get; set; }
/// <summary>
/// Gets or sets the publish date.
/// </summary>
/// <remarks>
/// If blank, the video will be published immediately.
/// </remarks>
[ApiMember(Name = "PublishDate",
AllowMultiple = false,
DataType = "date",
Description = "Publish date. If blank, the video will be published immediately.",
ParameterType = "form")]
public DateTime? PublishDate { get; set; }
}