• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


27 Mar, 2025

Updated at 12 May, 2025

.Net Core Filter works if query params is passed, otherwise it does not even hit breakpoint

I have added a filter to get data either from query params or from body, but breakpoint on filter is only hit when I have passed query params to method, else not

FilterCode:

public class GroupingCodeFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var httpContext = context.HttpContext;
            var request = httpContext.Request;

            if (request.Query.TryGetValue("grouping", out var groupingFromQuery))
            {
                httpContext.Items["grouping"] = groupingFromQuery.ToString();
            }
 
            else if (context.ActionArguments.TryGetValue("model", out var value) &&
                     value is GridParamsModel gridModel &&
                     gridModel.ParameterDictionary != null &&
                     gridModel.ParameterDictionary.TryGetValue("groupingCode", out var groupingCodeObj))
            {
                var groupingCode = groupingCodeObj?.ToString();
                if (!string.IsNullOrEmpty(groupingCode))
                {
                    httpContext.Items["grouping"] = groupingCode;
                }
            }

            await next();
        }
    }

Method on which filter works :

[HttpPost("AddRecord")] 
[GroupingCodeFilter]
[DynamicTask(typeof(TaxCalendarAPIService.Controllers.GridServiceControllers.MaintainPaymentTypesController), "GetDynamicAaddRecordTaskAttributeID", "grouping")] 
public async Task AddRecord([FromQuery] string grouping, [FromBody] CodeListDTO model) {} 

Method on which filter does not work :

[HttpPost("GetContent")]
[GroupingCodeFilter]
[DynamicTask(typeof(...), "GetDynamicTaskAttributeID", "grouping")]
public async Task GetContent([FromBody] GridParamsModel model)
{}

On GetContent Method filter works if I pass queryparams or if I comment DynamicTaskAttribute

I have even tried to register filter in startup and then use it as but nothing worked

[ServiceFilter(typeof(GroupingCodeFilter))]

GetMethod shows error that The give key 'grouping' was not present in the dictionary

Somebody please help. I am just tired of this