Hello!
I'd like to make a Custom ActionFilterAttribute and within that class use one of my application services, as well as the AbpSession.
How would I go about doing this?
Thank you!
4 Answer(s)
-
0
hi,
you can see the AbpAuditActionFilter . This is a good example and entry point of how to implement an action filter. And you can add your filter here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/299b8f59e118c11425bc467d9e54ab4e1232918d/src/Abp.AspNetCore/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs">https://github.com/aspnetboilerplate/as ... ensions.cs</a>
-
0
Hey alper, thanks for the reply, we're using Asp.Net MVC 5 not Core.
Can you please explain or provide an example for Asp.Net MVC 5?
Thank you!
-
0
Hey all, in case anyone needs, also for checking of best practices I did the following:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class TestAttribute : ActionFilterAttribute, ITransientDependency { private ITestAppService _iTestAppService; private IAbpSession _session; public TestAttribute() : this(IocManager.Instance.Resolve<ITestAppService>(), IocManager.Instance.Resolve<IAbpSession>()) { } public TestAttribute(ITestAppService service, IAbpSession session) { _iTestAppService = service; _session = session; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.Controller.GetType() == typeof(SpecificController)) { if (_iTestAppService.GetTestItemByUserId(_session.UserId.Value).TestListCheck.Count == 0) { filterContext.Result = new RedirectResult("RedirectToSpecificPage"); } base.OnActionExecuting(filterContext); } } }
I added this Attribute Class to a Filters directory within the area it's being used.
Then just added the attribute to the actions where restriction was required.
Please advise if there's a better practice way of achieving the same result.
Thank you!
-
0
For Http Action Filterexample, see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Api/WebApi/Auditing/AbpApiAuditFilter.cs">https://github.com/aspnetboilerplate/as ... tFilter.cs</a>
For Mvc Action Filterexample, see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Mvc/Web/Mvc/Auditing/AbpMvcAuditFilter.cs">https://github.com/aspnetboilerplate/as ... tFilter.cs</a>