locked
.net core 3.1 finding replacment for HttpContext.ActionContext.ActionArguments RRS feed

  • Question

  • User956626884 posted

    Hi,

    I am using webapi core v3.1 and trying to replace the old HttpContext.ActionContext.ActionArguments esp the Key and Value. 

    I am trying to add ActionContextAccessor into my webapi core 3.1. I have an ExceptionFilter and like to add ActionContextAccessor to access the controller method parameter key and value. The old .net core is httpContext.ActionContext.ActionArguments

    In my ExceptionFilter

    using Microsoft.AspNetCore.Mvc.Infrastructure;

    public class MyApiExceptionFilter : ExceptionFilterAttribute
    {
      public override void OnException(ExceptionContext context)
      {
         // I am checking what is in  the context object.
      }
    }

    I don't see ActionArguments but I do see ActionDescriptor which contains Parameters object. The Parameters
    object does have the parameter object I am searching but I don't see the values for the parameters.

    In my startup.cs I did add services.AddSingletop<IActionContextAccessor, ActionContextAccessor>();

    Any help is appreciated

    Tuesday, May 4, 2021 11:57 AM

Answers

  • User1535942433 posted

    Hi comicrage,

    As far as I think,you could inherit two interfaces: ExceptionFilterAttribute and IActionFilter. And get the value when calling OnActionExecuting event.

    Just like this:

    public class CustomExceptionFilter : ExceptionFilterAttribute, IActionFilter
        {
            private IDictionary<string, object> actionArguments { get; set; }
            public void OnActionExecuted(ActionExecutedContext context)
            {
                
            }
            public void OnActionExecuting(ActionExecutingContext context)
            {
                actionArguments=context.ActionArguments;
            }
            public override void OnException(ExceptionContext context)
            {
                var t=actionArguments;
            }
        }


    [CustomExceptionFilter]
    public IActionResult Index(int? id=98)
    {
    Directory.CreateDirectory("");
    return View(test);
    }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 8:11 AM