asp.net mvc put controllers into a separate project
Asked Answered
A

5

108

I'm just learning asp.net mvc and I'm trying to figure out how to move my controllers into a separate project. Typically when I have designed asp.net web apps before, I created one project for my models, another for my logic, and then there was the web.

Now that I'm learning asp.net mvc I was hoping to follow a similar pattern and put the models and controllers each into their own separate projects, and just leave the views/scripts/css in the web. The models part was easy, but what I don't understand is how to make my controllers in a separate project be "found". Also, I would like to know if this is advisable. Thanks!

Aleen answered 30/12, 2008 at 20:19 Comment(0)
D
92

First of all, it is certainly a good idea to put your model into a separate project. As you've discovered, this is trivial.

Regarding Controllers and Views, I don't see any obvious advantage to separating them for most basic projects, although you may have a particular need to do so in a particular application.

If you do choose to do this, then you will need to tell the framework how to find your controllers. The basic way to do this is by supplying your own ControllerFactory. You can take a look at the source code for the DefaultControllerFactory to get an idea for how this is done. Subtyping this class and overriding the GetControllerType(string controllerName) method may be enough to accomplish what you're asking.

Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Update: Read this post and the posts it links to for more info. See also Phil Haack's comment on that post about:

ControllerBuilder.Current.DefaultNamespaces.Add(
    "ExternalAssembly.Controllers");

...which is not a complete solution, but possibly good enough for simple cases.

Denicedenie answered 30/12, 2008 at 20:57 Comment(11)
Thanks Craig! This is exactly what I've been looking for. Does this information even exist on the web? I've been googling all over for it with not much luck. StackOverflow comes through again!Aleen
Are you going to be reusing the controllers in another project? If not, then there's no point to doing this.Iong
I agree, controllers handle user input, manipulate model, then pass data to the view. They are typically very specific to an application. Any logic that isn't specific to the app might be better off in a library or the model. But controllers in general ought to be with the web project.Zinovievsk
I have an error controller and views that are identical between two apps. It makes sense to me to have those in a single assembly that both apps can use.Alexandrite
Is it not easier to test the controllers and use IoC when the controllers are in a separate project - that would be the main reasonJaynajayne
@Chev, I don't think it makes any difference there. The testability of your controllers has more to do with how you code them, not where they live.Denicedenie
I want to separate out controller in separate project because project with Global.asax +views refer my core model project and respository .dlls for boot-strapping,and I dont want to use developer of controller to refer these core projects accidentally. Is it a good reason to separate controller in diff project?Tailpiece
The post mentioned in your update doesn't work for me. I believe this is the same post you were referring to?Mortenson
@Smudge202: Yes. Old post, though!Denicedenie
Yup, sorry for the necro. Your post was of use to me earlier though so an extra +1 from me. Ta! ;)Mortenson
Awesome! By reading your post that you referenced I was able to exactly accomplish what I wanted; I put my controller in a separate class library and I configured the routing to reference the name space.Grandsire
A
20

While it is reasonable to create your own ControllerFactory, I found it more convenient to define all my Controllers in each project, but derive them from Controllers in my Shared project:

namespace MyProject1.Controllers
{
   public class MyController : MySharedProject.Controllers.MyController
   {
      // nothing much to do here...
   }
}

namespace MySharedProject.Controllers
{
   public abstract class MyController : System.Web.Mvc.Controller
   {
      // all (or most) of my controller logic here...
   }
}

This has the added benefit that you have a place to put your Controller logic that differs from project to project. Also, it is easier for other developers to quickly find your Controller logic because the Controllers exist in the standard place.

Regarding whether this is advisable, I think it absolutely is. I've created some common Account Management logic that I want to share between projects that otherwise have very different business logic. So I'm sharing my Account and Admin Controllers, but the other Controllers are specific to their respective projects.

Axiomatic answered 13/2, 2013 at 23:34 Comment(8)
This worked very well, but I had to eliminate some redundant code to prevent a routing errorSherri
Hi , How i can manage routing in this type of project ? i want to use attribute routing ...Mycetozoan
You can use attribute routing just as you would normaly.Axiomatic
Not sure why this doesn't have more upvotes... much more elegant than the accepted answer, I think. Thanks!Druid
This does not work for me. Could you tell is this .Net Core or .Net Framework?Tiv
@HomayounBehzadian my answer was 4+ years ago, so it was not .Net Core. With that said, I don't see any reason this wouldn't work. Can you be more specific, what did you try and what happened?Axiomatic
@Axiomatic I found problem, MVC that referenced from library has different version from MVC assembly that referenced from MVC website! so my controller would not found at all!!! Now both class and website reference to specific version of asp.net MVC and all things are OK.Tiv
What do we need to do to generate Swagger documentation for Base Controllers?Metatarsal
F
4
  • Add the Class Library for your mvc project.
  • In the class add the following code(For u'r Controller Code)

    namespace ContactController
    {
    public class ContactController : Controller
    {
        public ActionResult Call()
        {
            ViewBag.Title = "Inside MyFirst Controller.";
            return View();
        }
    }
    

    }

  • On the mvc project view folder add the folder for Contact and create a Call.cshtml file. View Folder

  • Add the class library project reference into your main MVC project.

Reference

  • Finally to refer contact controller namespace into Route Config.

RouteConfig

Fidge answered 2/7, 2015 at 15:52 Comment(1)
That's slightly unfortunate that you have namespace with same name as the controller.Posterior
T
3

My problem solved after I updated System.Web.Mvc NuGet reference so MvcWebsite and Class Library use same System.Web.Mvc version

No need to add default namespaces

Tiv answered 9/7, 2018 at 8:16 Comment(1)
The problem was that MvcWebsite did not throw exception while present mvc assembly has lower version than class library's referenced mvc assemblyTiv
Q
1

The simplest form of separation I use is to retain the Views "as is" in the original MVC project but remove the Controllers. Then in a new ClassLibrary project add the Controller classes and ensure they inherit from Controller.

The MVC routing engine will automatically route to the Controllers in the ClassLibrary and the Controllers will automatically construct the Views from the original MVC project, provided you have your references and usings correctly in place.

I am using this architecture to implement an Html Reports module that can be compiled and deployed separately from the main solution. At last I am free from SSRS!

Quintic answered 26/11, 2013 at 5:52 Comment(1)
"at last I am free from SSRS" blog.codinghorror.com/the-best-code-is-no-code-at-allHanker

© 2022 - 2024 — McMap. All rights reserved.