public async Task Invoke(HttpContext context)
        {
            await _next(context);

            if (context.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
            {
                if (context.IsApiCall())
                {
                    var result = new ApiReturn <object>
                    {
                        Code    = (int)HttpStatusCode.Unauthorized,
                        Success = false,
                        Message = "Token geçersiz. Lütfen geçerli bir token kullanarak tekrar deneyin."
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = (int)HttpStatusCode.Unauthorized;
                    await context.Response.WriteAsync(result.ToString());
                }
                else
                {
                    context.Response.Redirect("/Auth/Login");
                }
            }
        }
        public async Task Invoke(HttpContext context)
        {
            await _next(context);

            if (context.Response.StatusCode == (int)HttpStatusCode.Forbidden)
            {
                if (context.IsApiCall())
                {
                    var result = new ApiReturn <object>
                    {
                        Code    = (int)HttpStatusCode.Forbidden,
                        Success = false,
                        Message = "Bu işlemi gerçekleştirmek için yetkiniz bulunmuyor."
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = (int)HttpStatusCode.Forbidden;
                    await context.Response.WriteAsync(result.ToString());
                }
                else
                {
                    context.Response.Redirect("/Auth/Login");
                }
            }
        }
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception ex)
            {
                if (context.IsApiCall())
                {
                    var result = new ApiReturn <object>()
                    {
                        Code    = (int)HttpStatusCode.InternalServerError,
                        Success = false,
                        Message = ex.Message
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    await context.Response.WriteAsync(result.ToString());
                }
                else
                {
                    var error = new ErrorVM
                    {
                        RequestId     = Activity.Current?.Id ?? context.TraceIdentifier,
                        Message       = ex.Message,
                        DetailMessage = ex.StackTrace
                    };

                    context.Response.Redirect("/Error/Index");
                }
            }
        }
        public void OnException(ExceptionContext context)
        {
            if (context.HttpContext.IsApiCall())
            {
                var result = new ApiReturn <object>()
                {
                    Code    = (int)HttpStatusCode.InternalServerError,
                    Success = false,
                    Message = context.Exception.Message,
                    Data    = context.Exception
                };

                context.HttpContext.Response.ContentType = "application/json";
                context.HttpContext.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                context.HttpContext.Response.WriteAsync(result.ToString());
            }
            else
            {
                var error = new ErrorVM
                {
                    RequestId     = Activity.Current?.Id ?? context.HttpContext.TraceIdentifier,
                    Message       = context.Exception.Message,
                    DetailMessage = context.Exception.StackTrace,
                    Errors        = new List <string>()
                };

                if (context.Exception.InnerException != null)
                {
                    error.Errors.Add(context.Exception.InnerException.Message);
                    error.Errors.Add(context.Exception.InnerException.StackTrace);
                }

                context.ExceptionHandled = true;
                context.Result           = new RedirectToActionResult("Index", "Error", error);
            }
        }