Is it possible to make separate dlls with MVC proj

2019-03-25 04:22发布

问题:

We have a big project developed in Asp.net MVC5. Our models and business logic are defined in separate class libraries. Now we need to add another module to an existing project but we want a separate dll.

This module also shares the most javascripts, css files and other files. That is the reason we don't want to separate MVC project.

Is there any why we can create separate dll for module basis. so we don't want deploy or touch other dlls.

回答1:

From your description, you say that the projects share CSS and JS files. This leads me to believe you are talking about a separate MVC website (possibly part of the larger corporate website). This can be easiest with the use of Areas. If you are not familiar with Areas, please read the following: https://msdn.microsoft.com/en-us/library/ee671793(VS.100).aspx

Of course using Areas will require you to deploy the whole site everytime one of the areas change, and you have mentioned that you want to avoid doing so.

If you don't want to use areas, and instead want to create another MVC project in the same solution, you can do that easily too. You can right click on the solution, add new project > ASP.NET web application > MVC to add the project. To share JS and CSS files between these two MVC projects, you will have to create a new solution folder (right click solution > Add new solution folder), and move your resource files to that folder. Inside each MVC project in your solution, you will add existing items and select those js/css resource files. This way if you change the css file, it will be reflected in both the projects.

For more information, read the following:

How do you share scripts among multiple projects in one solution?



回答2:

Yes you can, just add the logic classes to other class library project (you can have as many as you want), then add references of those class librarys to the mvc project. Don't forget to import the classes after in your code

Edit: I'm assuming you are using Visual Studio, if yes, you can go to File -> Create Project, this will create another project in the same solution.



回答3:

I don't know whether you tried with Managed Extensibility Framework (MEF) or not.. this framework works as you required ... I think below link will help you more

ASP.NET MVC Extensibility with MEF

How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API

http://www.codeproject.com/Articles/167321/MEF-with-ASP-NET-Hello-World



回答4:

Other people have posted answers regarding the use of Areas. Areas are great and good and helpful. They really benefit the project structure.

This module also shares the most javascripts, css files and other file

The title of your question is about .dlls, but I suspect the client-side resources are the main concern.

If you consider your webapp as having two distinct parts: server-side and client-side, you can use appropriate strategies to modularize each. Areas a great for organizing server-side code, but don't help the front-end.

Front-end package management options have expanded for ASP.NET 5. In addition to the traditional NuGet package manager, Bower and NPM are now supported. For example, consider how this article demonstrates installing jQuery via NPM. Here's another good article about setting up NPM, Bower, and Gulp in Visual Studio.

What to do: Take your existing client-side code and make a custom NPM or Bower package, and then use that package from one or more Asp.NET projects.



回答5:

I can suggest you two ways to organize your multi-module project.

Option 1 - Create Area per module, within same web project

One way to do it is, create separate Area within the same MVC project. So each module will have a separate area, with separate controllers, views, scripts etc. But,
(1) This will still create a single dll for the whole MVC project
(2) Sharing files across areas might not be very easy in some scenarios (you can keep all the scripts for all the modules in one shared directory though)

Option 2 - Create class library per module, merge after build

Another way is to create a single class library project per module. Add reference to the System.Web.Mvc and other libraries so that it can have controllers etc. Create your own views, scripts and other folders and populate with files as you need them.

Now, all your modules will build as separate projects, with the dll file and the javasvripts, htmls, csss, images etc. To make them all work as a single web application you can create a (only one) MVC web project, which will go to the IIS virtual directory and will be published as web.

To use all your separate modules from the same web, you can write post build events in all those libraries to copy the artifacts (dll, scripts etc.) into the main web, into corresponding folders (dll to \bin, javascript to \scripts etc.). So, after successful build, all the artifacts are available in the same web project, and that can be deployed as a single web with all the modules. Your post build scripts should look something like this

XCOPY "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\YourMainWebDirectory\Bin\" /Y
XCOPY "$(ProjectDir)Content"  "$(ProjectDir)..\YourMainWebDirectory\Content\"  /S /Y
XCOPY "$(ProjectDir)Scripts"  "$(ProjectDir)..\YourMainWebDirectory\Scripts\"  /S /Y
XCOPY "$(ProjectDir)Views"  "$(ProjectDir)..\YourMainWebDirectory\Views\"  /S /Y
XCOPY "$(ProjectDir)Images"  "$(ProjectDir)..\YourMainWebDirectory\Images\"  /S /Y

Now,
(1) You have separate dlls for separate modules
(2) Can directly share scripts and other files, as they will be in same location (after build)
(3) If you decide to remove a specific module from the web, just remove the post build event from that module (project) without affecting anything else. You can add that back at any time you please.

Your overall solution will look like

Module01.csproj => post build copy to main
    \Controllers
    \Scripts
    \Views
    \Contents
    \Images

Module02.csproj => post build copy to main
    \Controllers
    \Scripts
    \Views
    \Contents
    \Images

Models.csproj
    \...

Application.csproj
    \...

Main.Web.csproj => main web application hosted in IIS
    \Controllers
    \Scripts
    \Views
    \Contents
    \Images