Hi.
We created an API Endpoint same as the solution on this ticket (#3295). We managed to create an API Response with Status Code 304 with our desired return fields when we encountered an error in the validation. But when the process is all good and accepted with Status Code 200 , it shows different return and ignored the API Response we created. Please see the code snippets below for reference.
using block:
using Abp.Domain.Repositories;
using AutoMapper;
using PRJ.Application.Dto;
using PRJ.Core.Entities;
using PRJ.OrderTask.OrderTaskDto;
using PRJ.Utility;
using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Web.Script.Serialization;
using PRJ.Common.Dto;
using System.Net.Http.Headers;
Class implementation:
public class OrderUploadAppService : PRJAppServiceBase, IOrderUploadAppService
IF Status Code 200:
APIResponse apiResponse = new APIResponse()
{
ResponseCode = (int)HttpStatusCode.OK,
ResponseMessage = "Success",
OrderUploadId = orderUploadId,
ValidationMessages = validationMessage
};
var json = new JavaScriptSerializer().Serialize(apiResponse);
response = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(json)
};
return response;
IF Status Code 304:
APIResponse apiResponse = new APIResponse()
{
ResponseCode = (int)HttpStatusCode.NotModified,
ResponseMessage = "Validation unsuccessful, check validation messages.",
OrderUploadId = 0,
ValidationMessages = validationMessage
};
var json = new JavaScriptSerializer().Serialize(apiResponse);
response = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.NotModified,
Content = new StringContent(json)
};
return response;
Expected Return for 200:
{
"ResponseCode":200,
"ResponseMessage":"Success.",
"OrderUploadId":123,
"ValidationMessages":[]
}
Actual Return for 200:
{
"success": true,
"result": null,
"error": null,
"unAuthorizedRequest": false
}
Actual Return for 304:
{
"ResponseCode":304,
"ResponseMessage":"Validation unsuccessful, check validation messages.",
"OrderUploadId":0,
"ValidationMessages":[]
}
The question is why in 200, the API ignores the customized return. At break point, it passes the setup of API Response, JSON Serialization and encountered no errors but shows different response once done? And if 304, there's no problem displaying the desired return.
If the default response for 200 cannot be modified, how can I put a value (string or int or obj) on the result field? Something like this:
{
"success": true,
"result": "TESTResponseMessage",
"error": null,
"unAuthorizedRequest": false
}
Thank you.
1 Answer(s)
-
0
The response is being wrapped by MvcAjaxResponse for MVC and AjaxResponse for Web API. And the real result object is being sent to client as Result property of the response. You can add a attribute to suppress response wrapping for MVC and Web API . There are 2 attributes for that purpose: WrapResultand DontWrapResult. So basically you have to use DontWrapResult.