views:

582

answers:

3

ASP.Net MVC Controllers have several methods of forwarding control to another controller or action. However, all of them cause a client redirect, with a 302 and a new browser request.

Why is there no internal "call another controller's action" functionality, without getting the client involved? I'm pretty sure that the php CodeIgniter framework also lacks this functionality.

The best reason that I can come up with is that ultimately it's unnecessary, since any code you want to call into could be factored out into someplace common, and in ASP.Net MVC at least, the operation might be quite expensive. But a lot of people ask about this, and it seems like it would ultimately be a convenience.

So... lots of smart people designed these frameworks. There must be some good reasons for not having this?

+1  A: 

I suspect it may go against the REST idea, everything is a resource. If you wish to perform a server transfer, you can as well offer an extra url for that resource. This way the client will know on that specific url he will receive one particular representation of a resource, and not under circumstances something else. That makes sense actually.

Developer Art
I also thought about this. You'd obviously be consuming multiple resources (as defined by your routing) by doing an internal redirect, which is pretty unRESTful. I wasn't sure if this was a good enough reason to exclude the functionality, but it was high up there on my thought process.
womp
+3  A: 

In Codeigniter you can set custom routes and stuff to direct certain URLs to other controllers/actions, but I think you mean in the middle of a controller function to jump into another?

If there is any business logic you have, especially if it's going to be reused, it should go into a model, not a controller. You can also specify different views in a controller depending on some condition or something. If you have repeating code that doesn't 'fit' into a model, then it should probably end up as a static helper function, or in a library class.

So yeah, I think you're right on when you say:

The best reason that I can come up with is that ultimately it's unnecessary, since any code you want to call into could be factored out into someplace common

This forces you into staying within the MVC pattern.

Best to keep your controllers lightweight anyways.

mrinject
+2  A: 

Well, at least in ASP.NET MVC, controller actions are just C# methods called in creative ways. So you can just explicitly call another controller action, like so:

Sample controller:

public class SampleController : Controller
{
   public ActionResult Foo()
   {
      return View("foo");
   }

   public ActionResult ShowMeFoo()
   {
      return Foo();
   }
}

Now, I think in most cases one wouldn't want to do this. Considering urls as a RPC interface for your app, it should forward to a different url when getting different results. Doesn't apply to all cases, but can appy to most.

Wyatt Barnett
Very true. I guess I'm more concerned about transferring to another Controller using the Redirect methods, like RedirectToAction(), but this is a good way to avoid the redirect if you're using the same controller.
womp