Closed glombek closed 9 years ago
Atm for AJAX results an empty json object is returned, I could also return "nothing", wouldn't matter :)
What information would you need (except of the status code which is sent anyway)? Some friendly HTTP Status Code Message? The Exception message (which can also be logged in the application)?
And what content types should be supported? Plain text, JSON, XML, ... ?
Thanks, I couldn't find the code causing this!
I was expecting the same HTML response as a non-ajax request. This is important for using AJAX with the HTML5 History API, for example.
I'd suggest not looking at the IsAjaxRequest
method, perhaps respond to the Accepts
header? Perhaps return HTML by default, if the header allows. If the Accepts
header does not allow html/text
, try the same with plaintext, JSON and XML?
I can't think of a use case where you would want a JSON 404, surely the code 404 and the error message should be enough? In this case, I'd probably always return the HTML.
Is the reason it returns nothing with AJAX requests for saving bandwidth?
In this case, maybe return HTML if the Accepts
header allows, otherwise return empty string.
If this sounds like a sensible solution, I'll fork and pull request.
Sounds legit, so:
Iterate through the headers The first match sorted by quality parameter should win If HTML, just return the ViewResult If JSON / XML, return a simple object (HTTP Status Code, HTTP Message, Exception information**) If there is no match, return an empty (or HTML) result? What does the HTTP specification says here :)
\ If we want to return this information, we have to check IsCustomErrorEnabled so we do not expose sensible information in production mode.
Sounds sensible. I'll take a look over the weekend, hopefully.
The Controller now returns a JSON object with more information for AJAX requests:
string message = exception != null && !requestContext.HttpContext.IsCustomErrorEnabled ? exception.ToString() : string.Empty;
actionResult = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
httpStatusCode = renderedHttpStatusCode,
httpStatusMessage = renderedHttpStatusCode.ToString(),
message = message
}
};
Message is only set when custom errors are disabled.
You might add your support for other content types over the weekend though :)
Requests with the header
X-Requested-With: XMLHttpRequest
returns an empty string for the 404 page (and possibly others).