5

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?

asked Sep 8, 2009 at 20:16

3 Answers 3

4

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.

answered Sep 8, 2009 at 20:41
Sign up to request clarification or add additional context in comments.

Comments

4

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.

answered Sep 9, 2009 at 0:08

1 Comment

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.
2

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.

answered Sep 8, 2009 at 20:39

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.