VB .Net:Json 未使用 ASHX 返回
VB .Net: Json not returning using ASHX
我的代码没有返回 Json 响应时遇到问题。我以前从未使用 ASHX 构建过 API。我检查了正在创建的 Json 字符串,它看起来不错。
返回的序列化 json 如下所示:
"{""Error"":""Error: Import: Unknown SKU Received!""}"
这是应该返回的正确消息。相反,我收到 Bad Request 作为响应。不会在我的代码端产生任何错误。
代码片段:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim oImporter As Importer
Dim oResponse As Domain.Structures.Structures.ReturnResponse
Dim Input_sr As StreamReader
Dim sData As String
Dim bolError As Boolean = False
Dim obj As New Domain.Models.Order.Order
Dim sError As New order_response_error
Dim sResponse As New order_response_success
Try
Input_sr = New StreamReader(context.Request.InputStream)
sData = Input_sr.ReadToEnd
Input_sr.Close()
If context.Request.ContentType.StartsWith("application/json") Then
obj = JsonConvert.DeserializeObject(Of Domain.Models.Order.Order)(sData)
If obj.api_key = ConfigurationManager.AppSettings("api_key") Then
oImporter = New Importer(sData)
oResponse = oImporter.ImportOrder
context.Response.StatusCode = HttpStatusCode.OK
If oResponse.ReturnCode = "Error" Then
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = oResponse.ReturnCode + ": " + oResponse.Message
'sError.Error = String.Format(oResponse.ReturnCode, oResponse.Message)
Else
context.Response.StatusCode = HttpStatusCode.OK
sResponse.vendor_order_id = oResponse.Message
End If
Else
bolError = True
context.Response.StatusCode = HttpStatusCode.Unauthorized
sError.Error = "Error: Access Denied"
'sError.Error = String.Format("Error", "Access Denied")
End If
Else
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = "Error: Invalid content type"
'sError.Error = String.Format("Error", "Invalid content type")
End If
Catch ex As Exception
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
Utils.ErrorEmail("Order Error!", ex, AttachmentName:="Order.xml", AttachmentData:=sData)
sError.Error = String.Format("Error", ex.Message)
End Try
context.Response.ContentType = "application/json"
If bolError Then
context.Response.Write(JsonConvert.SerializeObject(sError))
Else
context.Response.Write(JsonConvert.SerializeObject(sResponse))
End If
End Sub
来自客户端的错误响应:
错误:
Cache-Control:
私人的
Content-Type: application/json;字符集=utf-8
服务器:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-SourceFiles: =?UTF-8?B?XFxhd2FyZGdyYXBoaWNzLmNvbVxkYXRhXFVzZXJzXG1ob2xtZXNcRGVza3RvcFxDb2xsYWdlXENvbGxhZ2VcQ29sbGFnZS5XZWIuUG9ydGFsXGFwaVx0ZkXN0xL?
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods:
获取,POST,选项
Access-Control-Allow-Headers: X-Requested-With,Content-Type
日期:
2019 年 10 月 14 日星期一 14:47:57 GMT
Content-Length: 48 字节
完成请求 HEADERS
漂亮的
Sec-Fetch-Mode: 科尔斯
来源:chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
Content-Type: application/json
接受:/
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie:AspxAutoDetectCookieSupport=1
您的代码中只有一个地方您 return 来自服务器的自定义消息,并带有 'BadRequest',在这些行中:
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = oResponse.ReturnCode + ": " + oResponse.Message
这意味着您的业务逻辑正在 returning 这个(我不能说是否恰当)。您需要跟踪业务逻辑以找到呈现错误 SKU 消息的逻辑。从这个角度来看,ASHX 看起来工作正常。
我的代码没有返回 Json 响应时遇到问题。我以前从未使用 ASHX 构建过 API。我检查了正在创建的 Json 字符串,它看起来不错。
返回的序列化 json 如下所示:
"{""Error"":""Error: Import: Unknown SKU Received!""}"
这是应该返回的正确消息。相反,我收到 Bad Request 作为响应。不会在我的代码端产生任何错误。
代码片段:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim oImporter As Importer
Dim oResponse As Domain.Structures.Structures.ReturnResponse
Dim Input_sr As StreamReader
Dim sData As String
Dim bolError As Boolean = False
Dim obj As New Domain.Models.Order.Order
Dim sError As New order_response_error
Dim sResponse As New order_response_success
Try
Input_sr = New StreamReader(context.Request.InputStream)
sData = Input_sr.ReadToEnd
Input_sr.Close()
If context.Request.ContentType.StartsWith("application/json") Then
obj = JsonConvert.DeserializeObject(Of Domain.Models.Order.Order)(sData)
If obj.api_key = ConfigurationManager.AppSettings("api_key") Then
oImporter = New Importer(sData)
oResponse = oImporter.ImportOrder
context.Response.StatusCode = HttpStatusCode.OK
If oResponse.ReturnCode = "Error" Then
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = oResponse.ReturnCode + ": " + oResponse.Message
'sError.Error = String.Format(oResponse.ReturnCode, oResponse.Message)
Else
context.Response.StatusCode = HttpStatusCode.OK
sResponse.vendor_order_id = oResponse.Message
End If
Else
bolError = True
context.Response.StatusCode = HttpStatusCode.Unauthorized
sError.Error = "Error: Access Denied"
'sError.Error = String.Format("Error", "Access Denied")
End If
Else
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = "Error: Invalid content type"
'sError.Error = String.Format("Error", "Invalid content type")
End If
Catch ex As Exception
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
Utils.ErrorEmail("Order Error!", ex, AttachmentName:="Order.xml", AttachmentData:=sData)
sError.Error = String.Format("Error", ex.Message)
End Try
context.Response.ContentType = "application/json"
If bolError Then
context.Response.Write(JsonConvert.SerializeObject(sError))
Else
context.Response.Write(JsonConvert.SerializeObject(sResponse))
End If
End Sub
来自客户端的错误响应:
错误:
Cache-Control:
私人的
Content-Type: application/json;字符集=utf-8
服务器:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-SourceFiles: =?UTF-8?B?XFxhd2FyZGdyYXBoaWNzLmNvbVxkYXRhXFVzZXJzXG1ob2xtZXNcRGVza3RvcFxDb2xsYWdlXENvbGxhZ2VcQ29sbGFnZS5XZWIuUG9ydGFsXGFwaVx0ZkXN0xL?
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods:
获取,POST,选项
Access-Control-Allow-Headers: X-Requested-With,Content-Type
日期:
2019 年 10 月 14 日星期一 14:47:57 GMT
Content-Length: 48 字节
完成请求 HEADERS
漂亮的
Sec-Fetch-Mode: 科尔斯
来源:chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
Content-Type: application/json
接受:/
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie:AspxAutoDetectCookieSupport=1
您的代码中只有一个地方您 return 来自服务器的自定义消息,并带有 'BadRequest',在这些行中:
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = oResponse.ReturnCode + ": " + oResponse.Message
这意味着您的业务逻辑正在 returning 这个(我不能说是否恰当)。您需要跟踪业务逻辑以找到呈现错误 SKU 消息的逻辑。从这个角度来看,ASHX 看起来工作正常。