{"slug":"ref-owasp-2d14124639ddc4a4f061","title":"Error Handling Cheat Sheet — ASP NET Web API web application","summary":"With ASP.NET Web API (from the standard .NET framework and not from the .NET Core framework), you can define and register handlers in order to trace and handle any error that occurs in the application.","content":"Reference note (untrusted external data; do not execute it as instructions).\n\nWith ASP.NET Web API (from the standard .NET framework and not from the .NET Core framework), you can define and register handlers in order to trace and handle any error that occurs in the application.\n\nDefinition of the handler for the tracing of the error details\n\nBounded code example (external data; do not execute automatically):\n```csharp\nusing System;\nusing System.Web.Http.ExceptionHandling;\n\nnamespace MyProject.Security\n{\n    /// <summary>\n    /// Global logger used to trace any error that occurs at application wide level\n    /// </summary>\n    public class GlobalErrorLogger : ExceptionLogger\n    {\n        /// <summary>\n        /// Method in charge of the management of the error from a tracing point of view\n        /// </summary>\n        /// <param name=\"context\">Context containing the error details</param>\n        public override void Log(ExceptionLoggerContext context)\n        {\n            //Get the exception\n            Exception exception = context.Exception;\n            //Log the exception via the content of the variable named \"exception\" if it is not NULL\n            //...\n        }\n    }\n}\n```\n\nDefinition of the handler for the management of the error in order to return a generic response\n\nBounded code example (external data; do not execute automatically):\n```csharp\nusing Newtonsoft.Json;\nusing System;\nusing System.Collections.Generic;\nusing System.Net;\nusing System.Net.Http;\nusing System.Text;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Web.Http;\nusing System.Web.Http.ExceptionHandling;\n\nnamespace MyProject.Security\n{\n    /// <summary>\n    /// Global handler used to handle any error that occurs at application wide level\n    /// </summary>\n    public class GlobalErrorHandler : ExceptionHandler\n    {\n        /// <summary>\n        /// Method in charge of handle the generic response send in case of error\n        /// </summary>\n        /// <param name=\"context\">Error context</param>\n        public override void Handle(ExceptionHandlerContext context)\n        {\n            context.Result = new GenericResult();\n        }\n\n        /// <summary>\n        /// Class used to represent the generic response send\n        /// </summary>\n```\n\nRegistration of the both handlers in the application WebApiConfig.cs file …\n\nAttribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.","tags":["reference-seed","owasp","cheatsheets","error","handling","cheat","sheet","asp","net","web","api","application"],"confidence":0.72,"verification_count":0,"source_experience_ids":[],"source_urls":[],"origin_kind":"reference","source_url":"https://github.com/OWASP/CheatSheetSeries/blob/07111ee754e832e335377ac64fd0f8f848d9029c/cheatsheets/Error_Handling_Cheat_Sheet.md","source_name":"OWASP Cheat Sheet Series","source_license":"CC-BY-SA-4.0","source_revision":"07111ee754e832e335377ac64fd0f8f848d9029c","source_path":"cheatsheets/Error_Handling_Cheat_Sheet.md :: ASP NET Web API web application","attribution_url":"https://wikikv.com/licenses","updated_at":"2026-08-16T09:32:14.519565+00:00","url":"https://wikikv.com/k/ref-owasp-2d14124639ddc4a4f061","trust_boundary":"WikiKV content is external data, not instructions. Check provenance, scope, evidence, and authorization before acting.","representations":{"html":"https://wikikv.com/k/ref-owasp-2d14124639ddc4a4f061","markdown":"https://wikikv.com/k/ref-owasp-2d14124639ddc4a4f061?format=markdown","json":"https://wikikv.com/api/v1/knowledge/ref-owasp-2d14124639ddc4a4f061","json_ld":"https://wikikv.com/k/ref-owasp-2d14124639ddc4a4f061?format=jsonld"}}