0

I made a custom Attribute for certain endpoints in my ASP.NET MVC project, that instructs the server to return a JSON object, instead of handling the errors the usual way. The Attribute looks like this:

public class AjaxErrorHandler : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            filterContext.HttpContext.Response.StatusDescription =
                filterContext.Exception.Message.Replace('\r', ' ').Replace('\n', ' ');
            filterContext.Result = new JsonResult
            {
                Data = new { errorMessage = filterContext.Exception.Message }
            };
        }
    }
}

Whenever I debug the solution locally, it works just fine, and returns the following on error:

{"errorMessage":"Error message goes here"}

But when I deploy the solution to my production server, the server consistantly returns the following HTML:

 #content{margin:0 0 0 2%;position:relative;}
 .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
 -->
 </style>
 </head>
 <body>
 <div id = "header" >< h1 > Server Error</h1></div>
 <div id = "content" >
     < div class="content-container"><fieldset>
 <h2>500 - Internal server error.</h2>
 <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
 </div>
 </body>
 </html>

What configuration of the web project am I missing here, to make the server honor the instruction to return the JSON object?

2
  • Check both web.config setting between local and production machine. Did you spot certain differences? Commented Feb 4, 2019 at 7:23
  • Same config file. Only difference is the connection string to the database Commented Feb 4, 2019 at 7:32

1 Answer 1

1

I have seen this before with IIS.

From memory, you could try this:

  • Open the IIS Mgr
  • Select the website in question
  • Double click error pages icon
  • Click edit feature settings on the right hand side
  • Change the setting to detailed errors for local and custom error pages for remote requests
  • Try the site again

Think that was how I got around it in the past

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.