Open Closed

Custom Data Filter Not Triggering on API Requests in ASP.NET Core #8332


User avatar
0
PerigisettiVenkateswaraRao created
  • ABP Framework version: v8.2.1

  • UI Type: Angular

  • Database System: EF Core (MySQL)

  • Tiered (for MVC) or Auth Server Separated (for Angular): Auth server separated angular

  • Exception message and full stack trace:

  • Steps to reproduce the issue:

I need to create a custom filter to filter my entity based on BranchId. The BranchId will be received as a List<long> from the request headers or parameters. Here's the approach I followed:

  1. Created IBranchEntity Interface:

    • Placed this interface in the Domain.Shared folder.

    • It includes a BranchId property, which I inherited in the relevant entity.

  2. Defined IBranchContext Interface:

    • This interface includes a CurrentBranchId property to store the branch ID.

    • Implemented the interface in a BranchContext class in the Http.Host project with the necessary logic.
      image.png

  3. Entity Framework Filtering Logic:

    • Created a file in the EntityFramework folder to define filtering logic for entities.
      image.png

  4. Service Registration:

    • Registered the IBranchContext in the HostModule.cs under the ConfigureServices method as follows:

      context.Services.AddHttpContextAccessor();
      context.Services.AddScoped<IBranchContext, BranchContext>();
      
  5. Database Context Changes:

    • Updated the ClinicServiceDbContext constructor to accept the IBranchContext.

      private readonly IBranchContext _branchContext;
      
      public ClinicServiceDbContext(
          DbContextOptions<ClinicServiceDbContext> options,
          IBranchContext branchContext
      ) : base(options)
      {
          _branchContext = branchContext;
      }
      
       protected override void OnModelCreating(ModelBuilder builder)
       {
           base.OnModelCreating(builder);
      
           builder.ConfigureClinicService();
      
           builder.ConfigureBranchFilterForEntities(_branchContext);
       }
      

After implementing the above, the filter is correctly identifying entities with the BranchFilter when the application is initially loaded. However, it is not being triggered for subsequent requests.


3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    you can check this document to know how to add a custom filter.

    https://abp.io/docs/latest/framework/infrastructure/data-filtering

  • User Avatar
    0
    PerigisettiVenkateswaraRao created

    you can check this document to know how to add a custom filter.

    https://abp.io/docs/latest/framework/infrastructure/data-filtering

    I have already gone through the documentation when i try to keep the below code in dbcontext getting no suitable method to overload error on CreateFilterExpression method

    protected bool IsBranchFilterEnabled => DataFilter?.IsEnabled() ?? false;
    
    protected int? CurrentBranchId => BranchContext?.CurrentBranchId; 
    
    protected override bool ShouldFilterEntity(IMutableEntityType entityType)
    {
        if (typeof(IBranchEntity).IsAssignableFrom(typeof(TEntity)))
        {
            return true; // Apply filter to all entities implementing IBranchEntity
        }
    
        return base.ShouldFilterEntity(entityType);
    }
    
    protected override Expression> CreateFilterExpression(ModelBuilder modelBuilder)
    {
        var expression = base.CreateFilterExpression(modelBuilder);
    
        if (typeof(IBranchEntity).IsAssignableFrom(typeof(TEntity)))
        {
            // Build the filter for BranchId
            Expression> branchFilter = e =>
                !IsBranchFilterEnabled || 
                (EF.Property(e, "BranchId") == CurrentBranchId || EF.Property(e, "BranchId") == null);
    
            // Combine the new filter with existing filters
            expression = expression == null ? branchFilter : QueryFilterExpressionHelper.CombineExpressions(expression, branchFilter);
        }
    
        return expression;
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    HI,

    could you share a test project with me? i will check it. shiwei.liang@volosoft.com

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on June 20, 2025, 11:20