This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14897|Reply: 0

[WebAPI] C# handles exceptions through filters

[Copy link]
Posted on 1/10/2019 10:07:35 AM | | |
/// <summary>
    Exception filter
    /// </summary>
    public class ExceptionFiter : ExceptionFilterAttribute
    {
        readonly JavaScriptSerializer ser = new JavaScriptSerializer();

        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            string Method = actionExecutedContext.Request.Method.Method;
            var dic = new Dictionary<string, object>();
            var logUrl = LogHelpers.Record(actionExecutedContext.Exception.ToString()); Generate an error log document
            dic.Add("success", false);
            dic.Add("message", string. Format("System Exception Log: {0}", logUrl)); Tell the error log address
            var json = ser. Serialize(dic);
            actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            actionExecutedContext.Response.Content = new StringContent(json, Encoding.UTF8);
        }
    }





Previous:js implementation to download files
Next:js time control
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com