Home > Software engineering >  Why JsonResult needs to implement both IActionResult and IStatusCodeActionResult?
Why JsonResult needs to implement both IActionResult and IStatusCodeActionResult?

Time:09-22

JsonResult's documentation (also we can see this if we F12 into JsonResult's definition in Visual Studio)

Implements IActionResult, IStatusCodeActionResult

My question is why does it need to implement both of these interfaces ?

IStatusCodeActionResult's doc says it already implemented IActionResult

CodePudding user response:

Because JsonResult needs the IStatusCodeActionResult property StatusCode, and the method ExecuteResultAsync (ActionContext context) only.

this is designed so that you can just call

public IActionResult ActionMethod() 

from the Asp.Net Controllers.

If they created the class like this:

public class JsonResult : IStatusCodeActionResult

Then you are required to change the action method to

public IStatusCodeActionResult ActionMethod()

Developers now would not have the flexibility of returning other responses base on the interactions with the user/client.

CodePudding user response:

Credits to @Tseng's comment.

The source for JsonResult here doesn't implement both interfaces.

  • Related