Web Api – Action Filter Attribute
Merhabalar ,
Bu yazımda ActionFilter sınıfından bahsedeceğim , bu sınıf attribute u bir action ın çalışmadan önce ve sonrasında ki anlarda yapılması istenen işlemleri yönetmemize yarayan bir sınıftır.
Örnek olarak bu Filter ı miras alarak kendi ActionFilterımızı oluşturalım ve kullanalım .
MyActionAttribute adında bir sınıf ekleyip ActionFilterAttribute sınıfından miras alıyorum. OnActionExecuting , OnActionExecuted metodların override ediyorum
OnActionExecuting : Action Çalışmadan önceki işlemler bu metotta tanımlanmalıdır.
OnActionExecuted : Action Çalıştıktan sonraki işlemler bu metotta tanımlanmalıdır
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace WebApiApp.Filters
{
public class MyActionAttribute : ActionFilterAttribute
{
//Action Çalışmadan önceki işlemler bu metodada
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
}
//Çalıştıktan sonraki işlemler bu metodada
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
}
}
}
actin işlemlerinden önce ve sonra olmak üzere bir loglama işlemi yapmak istiyorum.
log işleminde tutulacak bilgiler için bir entity log oluşturdum.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebApiApp.Entities
{
public class Log
{
[Key]
public int Id { get; set; }
public DateTime Time { get; set; }
[MaxLength(300)]
public string LogCaption { get; set; }
public string LogDetail { get; set; }
public bool IsBefore { get; set; }
}
}
IsBefore ile OnActionExecuting , OnActionExecuted methodunda çağırıp çağırmadığımız bilgisini tuttacağım.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApiApp.Entities;
namespace WebApiApp
{
public class Logger
{
public static void LogYaz(string message, string messageDetail)
{
//throw new NotImplementedException();
}
public static void LogYaz(Log log)
{
//throw new NotImplementedException();
}
}
}
LogYaz methodunu Log sınfı alacak şekilde overload ettim. daha sonrasında kendi yapınıza göre bu metodun içeriğini değiştirebilirsiniz.
MyActionAttribute.cs sınıfımızın son hali :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using WebApiApp.Entities;
namespace WebApiApp.Filters
{
public class MyActionAttribute : ActionFilterAttribute
{
//Action Çalışmadan önceki işlemler bu metodada
public override void OnActionExecuting(HttpActionContext actionContext)
{
StringBuilder sb = new StringBuilder();
foreach (var item in actionContext.ActionArguments)
{
sb.Append($"{item.Key}={item.Value.ToString()},");
}
Log log = new Log()
{
IsBefore = true,
LogCaption = $"{actionContext.ControllerContext.ControllerDescriptor.ControllerName} - {actionContext.ActionDescriptor.ActionName}",
Time = DateTime.Now,
LogDetail = sb.ToString()
};
Logger.LogYaz(log);
base.OnActionExecuting(actionContext);
}
//Çalıştıktan sonraki işlemler bu metodada
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
Log log = new Log()
{
IsBefore = false,
LogCaption = $"{actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName} - {actionExecutedContext.ActionContext.ActionDescriptor.ActionName}",
Time = DateTime.Now,
LogDetail = (actionExecutedContext.Response.Content as ObjectContent).ObjectType.FullName
};
Logger.LogYaz(log);
base.OnActionExecuted(actionExecutedContext);
}
}
}
Örnek Kodda göreceğiniz şekilde actionContext, actionExecutedContext parametrelerindeki verileri okuyarak istediğimiz şekilde bir log verisi toplamış olduk.
Bu attribute ü kullanmak istediğimizde istediğimiz controller ın veya Action ın üzerine yazarak kullanabilirsiniz.
Örnek :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApiApp.Filters;
namespace WebApiApp.Controllers
{
// [RequireSSL]
[MyError]
public class ValuesController : ApiController
{
// GET api/values
[MyAction]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[MyAction]
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}

Şeklinde kullanabiliriz.
Örnek Sonuç :



