Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read values from Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor

Tags:

asp.net-core

As the question points out i am trying to read route values from it such as the id or other parameters.

I made this base class

public abstract class PermissionAttributeBase : Attribute, IAsyncAuthorizationFilter
{
    /// <inheritdoc />
    public async Task OnAuthorizationAsync(AuthorizationFilterContext filterContext)
    {
        if (filterContext.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
        {
            if (controllerActionDescriptor.MethodInfo.IsDefined(typeof(AllowAnonymousAttribute)))
                return;

            if (!await IsAuthorizedAsync(new PermissionContext(filterContext, controllerActionDescriptor)))
                filterContext.Result = new ForbidResult();
        }
    }

    protected abstract Task<bool> IsAuthorizedAsync(PermissionContext permissionContext);
}

However i can't seem to find a way to read the values from it. Are the values not available at the point of authorization perhaps?

I have tried many way but even something like this:

var read = permissionContext.FilterContext.ModelState.TryGetValue("id", out var val3);

returns no value at all.

like image 818
Dbl Avatar asked Feb 05 '26 10:02

Dbl


1 Answers

For MVC The parameter values for action are available on the ActionExecutingContext not on ControllerActionDescriptor.

Try ActionExecutingContext.ActionArguments

like image 89
ameya Avatar answered Feb 13 '26 04:02

ameya