Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error message with HTTPStatusCodeResult & jQuery

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);
                     }
                 },
like image 232
MrBliz Avatar asked Apr 27 '12 09:04

MrBliz


People also ask

How do I display custom error messages?

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.

How to display error message in custom error page asp net?

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.

How do I return an error in Actionresult?

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.

What is HttpStatusCodeResult?

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.


1 Answers

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.

like image 76
MrBliz Avatar answered Oct 25 '22 17:10

MrBliz