Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Value from ObjectResult

I have a filter like so:

public class Err : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var result = context.Result;
    }
}

result is an object of Microsoft.AspNetCore.Mvc.BadRequestObjectResult. It contains a StatusCode and a Value, but when I try to extract them like so: context.Result.Value, I get this error:

Error CS1061 'IActionResult' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'IActionResult' could be found.

like image 549
Aaron Avatar asked Oct 15 '25 23:10

Aaron


1 Answers

That is simple - property Result of ActionExecutedContext has IActionResult type which doesn't have property Value. You can cast it to BadRequestObjectResult to get access to Value property:

public class Err : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var result = context.Result as BadRequestObjectResult;
        // you can access result.Value here
    }
}
like image 89
Alex Riabov Avatar answered Oct 17 '25 11:10

Alex Riabov