2

I have some methods defined in a controller which most of them returns JsonResult, and some returns PartialViews.

So I have two questions,

1) Is it a good practice to access those methods that return JsonResult from other controllers?

2) Is it a good practice to access those methods that return ActionResult from other controllers ?

Thanks :)

Shanid
  • 177
  • 1
  • 6

1 Answers1

3

It's not a question of good and bad. Sometimes it's just necessary. The most common ways are:

  1. Define good interfaces between controllers that must communicate and let controllers exchange informations directly: this looks like your approach since one controller provides methods for retrieving jsons from the outside. It's not wrong but i prefer the second approach because it leads to more modularity.
  2. Detach the methods needed by both controllers and put them into a separate class that will act as a middleware. In this way the two controllers communicate through this third class. The cons of this solution is that when your controller has the json or the ActionResult you have to remember to update the new class so other controllers can access the information: to force a programmer to "remember" to update the new class you can code an interface implemented by controllers with methods like void jsonWrite(Json jsn) and Json jsonRead() .

Hope this helps

  • Yeah, the second method was the one I wanted to go with. Now what I've done is the first method, but without interfaces. By directly calling them like new myClass().myMethod();. Is this bad ? – Shanid Jun 01 '16 at 08:54
  • 1
    you can make myMethod static so you don't have to instantiate an object to call it – JoulinRouge Jun 01 '16 at 09:35