【发布时间】:2015-02-24 22:16:45
【问题描述】:
使用 asp.net web api v2,我有一个有效的 POST 方法,我能够从不同的应用程序发布自定义类型,并且通过使用 JSONConvert,我能够反序列化它并在我的 POST 方法主体中使用它。
但是,我的 POST 的参数必须是“object”类型,否则找不到参数(null)。
为什么会这样?理想情况下,我会将自定义类型作为参数类型,以便我的 API 文档可以填充正确的请求信息,因为它会根据使用的参数类型自动生成 API 文档(看不到覆盖它的方法 - - 如果可以的话,那就太好了)。
请参阅下面的代码——如果“incomingInformation”是“RemoteFileInfo”类型而不是“object”类型,当我尝试对其进行 .toString() 时会引发空异常。
[Route("api/xx/uploadfiletoalfresco/")]
[HttpPost()]
public ResultStruct UploadFileToAlfresco(object incomingInformation)
{
JObject deserializedJObject = (JObject)JsonConvert.DeserializeObject(incomingInformation.ToString());
SA.Services.RemoteFileInfo convertedRemoteFileInfo = deserializedJObject.ToObject<SA.Services.RemoteFileInfo>();
...
这是我在发送应用程序 (vb.net) 上的示例代码 - 内容类型设置为 application/json 并在发送前进行序列化
Dim req As WebRequest = WebRequest.Create(_restEndpointURL & "/uploadfiletoalfresco/")
req.ContentType = "application/json"
req.Method = "POST"
Using sw As New StreamWriter(req.GetRequestStream())
Dim ser As New JavaScriptSerializer
Dim serJSON = ser.Serialize(JsonConvert.SerializeObject(remoteFileInfo))
sw.Write(serJSON)
sw.Flush()
sw.Close()
End Using
下面是我的 remoteFileInfo 类型,它在接收应用程序和发送应用程序上都以这种方式声明。通过 JsonConvert.SerializeObject 方法发送前转换为 JSON 字符串
Partial Public Class RemoteFileInfo
Public CategoryID As Integer
Public FileName As String
Public Length As Long
Public Note As String
Public SFSubmissionID As String
Public SourceInstance As String
Public Subject As String
Public UserID As Integer
Public Visibility As Boolean
Public riskID As Integer
Public fileByteArray As Byte()
End Class
接收应用定义:
public class RemoteFileInfo
{
public int CategoryID;
public string FileName;
public long Length;
public string Note;
public string SFSubmissionID;
public string SourceInstance;
public string Subject;
public int UserID;
public bool Visibility;
public int riskID;
public Byte[] fileByteArray;
}
来自发送应用程序的 JSON 示例:
"{"CategoryID":2,"FileName":"Scrum postponed until this afternoon .msg","Length":62976,"Note":"asdf","SFSubmissionID":"006E000000OuYxP","SourceInstance":"Addin","Subject":"Scrum postponed until this afternoon ","UserID":0,"Visibility":true,"riskID":0,"fileByteArray":"VERY LONG STRING"}"
来自提琴手的完整 JSON:
POST http://yyyy/api/xxx/uploadfiletoalfresco/ HTTP/1.1
Content-Type: application/json
Host: yyyyy
Content-Length: 84273
Expect: 100-continue
Connection: Keep-Alive
"{\"CategoryID\":2,\"FileName\":\"Scrum postponed until this afternoon .msg\",\"Length\":62976,\"Note\":\"asdf\",\"SFSubmissionID\":\"006E000000OuYxP\",\"SourceInstance\":\"Addin\",\"Subject\":\"Scrum postponed until this afternoon \",\"UserID\":0,\"Visibility\":true,\"riskID\":0,\"fileByteArray\":\"VERY LONG STRING - user edited this is not how it looks in fiddler!\"}"
【问题讨论】:
-
这可能与不正确的 ContentType 有关。有关示例,请参见 stackoverflow.com/questions/20226169/…。
-
这不是您所要求的,但您可以输入
JObject _incomingInformation = incomingInformation as JObject;这会将 _incomingInformation 识别为 typeof(JObject) 并使用传入Information 参数传递的值。 -
@B2K 请查看已编辑的帖子——我确实有您链接帖子中总结的 application/json 类型。您看到的任何其他可能导致此问题的原因?
-
您没有提供足够的信息来提供明确的答案。你发的json数据和RemoteFileInfo的定义在哪里?
-
@B2K RemoteFileInfo 的定义已添加。如果有帮助,我可以粘贴一个示例原始 json
标签: c# json rest data-structures asp.net-web-api