Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call controllers from one project to another project

I'm using Asp.net MVC4 with razor. I want to know how to call a controller from one project to another project in a same solution. (I'm new to MVC4)

solution explorer screenshot

like image 594
tishantha Avatar asked Oct 04 '13 03:10

tishantha


People also ask

Can we call controller method from another controller?

Yes, you can call a method of another controller. The controller is also a simple class. Only things are that its inheriting Controller Class. You can create an object of the controller, but it will not work for Routing if you want to redirect to another page.

Can two controllers have same view?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.

Can a controller have multiple models?

Question: Can there be two+ models in 1 controller? Answer: yes. Create a wrapper model, put other two models in it.


1 Answers

You can simply add your controllers to another project (class lib or MVC project, etc...) We have a couple projects that share controllers(webAPI as well as MVC). I typically use area constraints for the API controllers and namespace constraints for MVC controllers- especially if you have something like a base HomeController.cs used for some projects and you want to override it in just one particular MVC application project.

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Common.MVC.Controllers" }
        );
like image 91
bradodarb Avatar answered Sep 23 '22 09:09

bradodarb