I have a controller action that returns some JSON results to the jQuery Full Calendar plugin. I return a HTTPStatusCodeResult
with a custom error message if there was an error, But I can't get the custom error message to display. All that's is displayed in the alert box is the default Http status (ie: 'Forbidden', or 'Internal Server Error')
Controller code that returns the error messages
else if(id != CurrentUser.UserId)
{
return new HttpStatusCodeResult(403, "You are not authorised to view this.");
}
else
{
return new HttpStatusCodeResult(500, "There was an error on the server.");
}
jQuery code
$(document).ready(function () {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
height: 600,
eventSources: [{
url: events,
type: 'Get',
error: function(response, status, error) {
alert(response.statusText);
}
}],
allDayDefault: false,
selectable: true,
eventClick: function (event) {
if (event.url) {
$('#details').load(event.url);
}
},
To display a custom error page with an appropriate error code, use the <httpErrors> section only, and do not use the <customErrors> section. Add the following <httpErrors> section under <system. webServer> section, as shown below.
When an unhandled exception arises in an ASP.NET application one of three types of error pages is displayed: The Exception Details Yellow Screen of Death error page, The Runtime Error Yellow Screen of Death error page, or. A custom error page.
You should be logging the relevant information to your error log so that you can go through it and fix the issue. If you want to show the error in the form user submitted, You may use ModelState. AddModelError method along with the Html helper methods like Html.
HttpStatusCodeResult(HttpStatusCode, String) Initializes a new instance of the HttpStatusCodeResult class using a status code and status description. HttpStatusCodeResult(Int32) Initializes a new instance of the HttpStatusCodeResult class using a status code.
Actually there wasn't any problem in the code at all. The problem was with the ASP.NET development Web Server in Visual Studio. I switched to using IIS express and it works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With