1

I'm looking to create a new MVC site, and one of the key problems I'm trying to solve is sharing code between a public area (web-facing) and an internal area.

Basically we have two applications, one that is the public web site that users access, and another that will be used internally at our company to view some of the same information that is also visible on the public site.

I would want to keep them as two separate sites (projects) because we have different teams of people working on each, and because we want to use different authentication formats (Forms for the public, Windows for the internal).

However, we also want to be able to share some of the code (views, controllers) between the two sites. How could we set up these two MVC sites such that one MVC site could reference a view and / or controller in another project?

For instance, we will have a view that will enable internal users to see transaction history of our public site users. If we have created that controller/view in the internal site and later we wanted to add the in the public site, how could we re-use this same view on the public site?

7
  • 1
    You can encapsulate your controllers and views in an assembly and then share that just like any library. You can also put your core logic into libraries and then wrap your controllers around that. Commented Jun 26, 2014 at 14:30
  • Does the routing configuration of MVC know how to handle that? Commented Jun 26, 2014 at 14:32
  • Routing is not specific to MVC. The routing actually hands the request off to mvc and that is what starts the mvc process. Commented Jun 26, 2014 at 14:33
  • 1
    If you're trying to put controller class in a shared assembly I think you need some config for the routes to tell it which namespace to look into. Commented Jun 26, 2014 at 14:34
  • You're going to need some config for your routes anyways. Commented Jun 26, 2014 at 14:34

3 Answers 3

1

Technically, as others have already said, you can put everything you want shared in a class library project, and then reference this project from your other projects. Views are a bit different as, in order to share them between projects, you will have to compile them. You'll need Razor Generator for that.

Practically, though, it's going to be rare that a view should be shared between projects (aside from something like a layout), and virtually non-existent that you should share a controller. If the overlap is that great, it's an argument that the two projects should not in fact be two projects, but one. Controllers and views are so customized to the application that sharing them just doesn't make sense most of the time. It is however a very good idea to share your models and other classes.

Sign up to request clarification or add additional context in comments.

5 Comments

This is part of my ongoing research for this upcoming project. I'm of the mindset that we should keep the sites in a single code base, because they will be sharing code so much, but I'm exploring options for keeping them separate. Thanks for your input
Single solution, definitely. However, if you have one internal site and one external one, those should be separate projects. You can achieve some degree of access control in IIS if they are one, but as two, you can you put them on entirely different servers in entirely different subnets if necessary. That gives you a great deal more options for security: internal site goes to an internal server behind a firewall, external site goes into a DMZ separated from the rest of your infrastructure.
My argument was merely that you should probably just create separate controllers, for example, instead of trying to shoehorn functionality from one into the other.
if i were to use Razor Generator to share my views to a different project, would I keep them in a single MVC project, so that I can easily scaffold, etc, and share them with Razor Generator selectively? Or would it be better to put the views in a class libary? -EDIT: ah, on codeplex for Razor Generator they recommend using an MVC project instead of a Class Library
It's essentially the same. The only reason that they recommend an MVC project is that you then don't have to worry about installing some of the nuget packages that come by default with an MVC project. Either way will work, though.
0

2 approaches I can think of:

First option is to use MVC Area. It gives you different controller/view for each area, while sharing the same Model. You can also encapsulate common methods in a Helper class.

Second option, create 3 projects. One is a .NET library containing the common logic, Models, validations, sending emails etc. Then create 2 more MVC projects, one for each site, and reference the project.

Comments

0

Look at using Areas within your site (this would allow you to keep the projects independent, but together as one final solution). Also, look at compiling your views in another library so you can share them between projects.

Some references:

1 Comment

@Mike: Nesting was intentional as it relates to compiling views.

Your Answer

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.