Aaron
Aaron

Reputation: 1455

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.

Upvotes: 4

Views: 8163

Answers (1)

Alex Riabov
Alex Riabov

Reputation: 9195

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
    }
}

Upvotes: 5

Related Questions