1

I have a ASP.NET Core 2.1 (API) application running in Azure Web App.

My application errors follow a standard json structure. When there is an error (for example a 500) in IIS, it returns a xhtml page with the description of the error.

How can I set Azure Web App, or my application, so that on IIS errors, it returns a json string defined by me.

Thanks in advance.

Victor
  • 23,172
  • 30
  • 86
  • 125
  • I would advise you to configure a custom exception handling middleware. See here for more details https://stackoverflow.com/questions/38630076/asp-net-core-web-api-exception-handling – Riscie Oct 18 '18 at 13:36
  • @Riscie Thanks. I have custom exception handling middleware, but these are not application errors but IIS (server) ones. – Victor Oct 18 '18 at 16:01

1 Answers1

0

We can use this method to achieve this: IApplicationBuilder.UseStatusCodePagesWithRedirects()

Here is a sample for your reference:

We can create Controller to handle the error output:

 [Route("Home/Error/{statusCode}")]
 public IActionResult Error(int statusCode)
 {
        JsonResult jsonResult = new JsonResult(new object());
        return jsonResult;
 }

Then We can use it in Configure(IApplicationBuilder app, IHostingEnvironment env) as below:

app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");

Hope this would be helpful to you.

Lee Liu
  • 1,981
  • 1
  • 12
  • 13