Home > Enterprise >  How can I access members of a JsonResult?
How can I access members of a JsonResult?

Time:11-16

For reasons, I am having to to try and check if the content of a JsonResult is simply "true" however I cannot find a way to access it. The object itself shows that the data is there in the object under "Value", however, typing ".Value" on the end of the object is invalid.

(I cannot simply refactor this so that I'm getting a regular string return instead of JsonResult as that would involve a lot of other code changes)

enter image description here

CodePudding user response:

In your case you can use this code

var result= await IsNot....;

if ( result.Value!=null && bool.TryParse(result.Value.ToString(), out var success))
    if ( success ) { };

but if you had in your action this code

return  new JsonResult (true);

you would not have to convert

var success = result.Value as bool?;

if ((success != null) && (bool) success) { };
  • Related