Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Action filters for services. #4675


User avatar
0
dreamsmiths created

Good day,

Is there a way in the boilerplate to implement a custom filter for service functions similar to an "action filter in MVC" ?

I want to achieve the following:

When the service API function is called ( /api/services/app/someService/Get) I want to check a cached value and depending on the value I want to continue with the API call or throw an exception to the user.

Thanks in advance.


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can add it like below in your Startup.cs;

    services.AddMvc(options =>
    {
    	options.Filters.Add(new YourCustomActionFilter());
    });
    

    Here is a sample action filter <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs">https://github.com/aspnetboilerplate/as ... nFilter.cs</a>.

  • User Avatar
    0
    dreamsmiths created

    Good day.

    Thanks for your feedback. :)

    I have done the following:

    Created the filter within the Web.Core project
    Resisted the the filter in the Web.Host startup project.

    I need to use the filter in the Application project and the Application project does not reference the Web.Host startup How can I achieve this?

    Please advice ?

    Thanks.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @dreamsmiths in AspNet Zero's ASP.NET Core versions, your app services will be considered as Controllers. So, when you add your filter to Filters list, it should be called automatically when you make a request to " /api/services/app/someService/Get". So, I couldn't understand why you need to use it in your app module.

  • User Avatar
    0
    dreamsmiths created

    Good day.

    Thanks for your reply. That works for all app services endpoints. :D But I want the filter to execute on specific endpoint methods in my app services. Like below:

    [TestFilter]
    public async Task<RegisterOutput> Register(RegisterInput input)
    {
    }
    

    How can I achieve this? Thanks in advance.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @dreamsmiths,

    You can define your own attribute, add it to app service methods/app services you want and then you can check this attribute in your custom filter and act accordingly.

  • User Avatar
    0
    dreamsmiths created

    Thanks in a million for your help.