I'm working on a .NET Framework 4.8 project using Web Forms (the one with .aspx pages). During development, I used a WebMethod tasked with fetching and validating data, returning either a success (status code 200) or an error (status code 400) response.
During development, everything worked as expected: it returned the result on success, and when there was a user-related error, it returned a message with guidance that was then displayed in a screen popup to inform the user (this message could be any of numerous validation-related issues).
The problem started after I deployed to production: now, when a 4XX error occurs, it simply returns "Bad Request," and I'm not sure why.
My web method looks like this (some parts are edited for security reasons):
[WebMethod]
public static string List(String codeConstruction, String code, String searchText, String from, String to, String const)
{
DateTime dateFrom = nUtil.RetornaDataAntiga();
DateTime dateTo = nUtil.RetornaDataFutura();
var custActive = const == "1";
if (!String.IsNullOrEmpty(de))
{
try
{
dateFrom = DateTime.Parse(de);
}
catch (Exception)
{
dateFrom = nUtil.RetornaDataAntiga();
}
}
if (!String.IsNullOrEmpty(ate))
{
try
{
dateTo = DateTime.Parse(ate);
}
catch (Exception)
{
dateTo = nUtil.RetornaDataFutura();
}
}
if (!custActive)
{
var dayFilter = 365;
if ((String.IsNullOrEmpty(code))
&& (String.IsNullOrEmpty(codeConstruction))
&& (String.IsNullOrEmpty(searchText))
&& ((dateTo - dateFrom).TotalDays > dayFilter))
{
return nUtil.RespostaJson(new { mensagem = "Not Enough Filters" }, 400);
}
}
List<eHistoricoCompras> lst = new List<eHistoricoCompras>();
using (var db = new dbModule())
{
lst = db.ListThings()
}
return nUtil.RespostaJson(lst);
}
This is the method that creates the response:
public static String RespostaJson<T>(T objeto, int statusCode = 200)
{
if (statusCode == 0)
{
statusCode = 500;
}
HttpContext.Current.Response.StatusCode = statusCode;
return JsonConvert.SerializeObject(objeto);
}
Frontend AJAX Call And my call is like this:
JavaScript
$.ajax({
type: "POST",
url: url,
contentType: 'application/json; charset=utf-8',
data: options.body ? JSON.stringify(options.body) : null,
dataType: 'json',
success: (data, status, xhr) => {
if (!data.d) {
response = { data: "", error: null, status: xhr.status };
} else {
const obj = JSON.parse(data.d);
response = { data: obj, error: null, status: xhr.status };
}
if (options.popupOnSuccess) {
showFastAlertWithStatus(response);
}
return resolve(response);
},
error: (xhr, status, error) => {
if (xhr.status > 499) {
response = { data: null, error: xhr.responseJSON, status: xhr.status };
}
else {
if (!xhr.responseJSON) {
response = { data: null, error: { mensagem: status }, status: xhr.status === 200 ? 500 : xhr.status };
}
else if (!xhr.responseJSON?.d) {
let errorObj = xhr.responseJSON;
if (!xhr.responseJSON?.mensagem && xhr.responseJSON?.Message) {
errorObj = { mensagem: xhr.responseJSON?.Message }
}
response = { data: null, error: errorObj, status: xhr.status };
}
else {
const obj = JSON.parse(xhr.responseJSON.d);
response = { data: null, error: obj, status: xhr.status };
}
}
if (options.popupOnError) {
showFastAlertWithStatus(response);
}
return resolve(response);
}
});
I tried checking if it could be a web.config setting in production that wasn't in my development environment, but both already had the same structure.
The response I get (happens in production):
The response I want (happens in development):

