简体   繁体   English

是否可以使用MVC项目制作单独的dll?

[英]Is it possible to make separate dlls with MVC project?

We have a big project developed in Asp.net MVC5. 我们在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. 现在我们需要向现有项目添加另一个模块,但我们需要一个单独的dll。

This module also shares the most javascripts, css files and other files. 该模块还共享最多javascripts,css文件和其他文件。 That is the reason we don't want to separate MVC project. 这就是我们不想分离MVC项目的原因。

Is there any why we can create separate dll for module basis. 有没有为什么我们可以为模块基础创建单独的DLL。 so we don't want deploy or touch other dlls. 所以我们不想部署或触摸其他dll。

From your description, you say that the projects share CSS and JS files. 根据您的描述,您说项目共享CSS和JS文件。 This leads me to believe you are talking about a separate MVC website (possibly part of the larger corporate website). 这让我相信你正在谈论一个单独的MVC网站(可能是更大的公司网站的一部分)。 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 如果您不熟悉区域,请阅读以下内容: 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. 如果您不想使用区域,而是想在同一个解决方案中创建另一个MVC项目,那么您也可以轻松完成。 You can right click on the solution, add new project > ASP.NET web application > MVC to add the project. 您可以右键单击解决方案,添加新项目> ASP.NET Web应用程序> MVC以添加项目。 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. 要在这两个MVC项目之间共享JS和CSS文件,您必须创建一个新的解决方案文件夹(右键单击解决方案>添加新的解决方案文件夹),然后将资源文件移动到该文件夹​​。 Inside each MVC project in your solution, you will add existing items and select those js/css resource files. 在解决方案中的每个MVC项目中,您将添加现有项目并选择那些js / css资源文件。 This way if you change the css file, it will be reflected in both the projects. 这样,如果更改css文件,它将反映在两个项目中。

For more information, read the following: 有关更多信息,请阅读以下内容:

How do you share scripts among multiple projects in one solution? 如何在一个解决方案中共享多个项目中的脚本?

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. 是的,您可以,只需将逻辑类添加到其他类库项目(您可以拥有任意多个),然后将这些类库的引用添加到mvc项目中。 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. 编辑:我假设您正在使用Visual Studio,如果是,您可以转到文件 - >创建项目,这将在同一解决方案中创建另一个项目。

I can suggest you two ways to organize your multi-module project. 我可以建议您组织多模块项目的两种方法。

Option 1 - Create Area per module, within same web project 选项1 - 在同一个Web项目中为每个模块创建区域

One way to do it is, create separate Area within the same MVC project. 一种方法是在同一个MVC项目中创建单独的区域 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 (1)这仍然会为整个MVC项目创建一个单独的dll
(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) (2)在某些情况下,跨区域共享文件可能不是很容易(您可以将所有模块的所有脚本保留在一个共享目录中)

Option 2 - Create class library per module, merge after build 选项2 - 为每个模块创建类库 ,在构建后合并

Another way is to create a single class library project per module. 另一种方法是为每个模块创建一个class library项目。 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. 添加对System.Web.Mvc和其他库的引用,以便它可以具有controllers等。创建自己的视图,脚本和其他文件夹,并根据需要填充文件。

Now, all your modules will build as separate projects, with the dll file and the javasvript s, html s, css s, 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. 现在,所有模块都将构建为单独的项目,包括dll文件和javasvript s, html s, css ,images等。要使它们全部作为单个web application工作,您可以创建一个(仅一个) MVC Web项目,它将转到IIS虚拟目录,并将作为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.). 要使用来自同一Web的所有单独模块,您可以在所有这些库中编写后期构建事件,以将工件(dll,脚本等)复制到主Web中,并复制到相应的文件夹(dll到\\ bin,javascript到\\ scripts)等等。)。 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. 因此,在成功构建之后,所有工件都可以在同一个Web项目中使用,并且可以将其部署为包含所有模块的单个Web。 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 (1)您有单独的dll用于单独的模块
(2) Can directly share scripts and other files, as they will be in same location (after build) (2)可以直接共享脚本和其他文件,因为它们将位于同一位置(构建后)
(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. (3)如果您决定从Web中删除特定模块,只需从该模块(项目)中删除post build事件,而不会影响其他任何事件。 You can add that back at any time you please. 你可以随时添加它。

Your overall solution will look like 您的整体solution将如下所示

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

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 此模块还共享最多javascripts,css文件和其他文件

The title of your question is about .dlls, but I suspect the client-side resources are the main concern. 你的问题的标题是关于.dlls,但我怀疑客户端资源是主要问题。

If you consider your webapp as having two distinct parts: server-side and client-side, you can use appropriate strategies to modularize each. 如果您认为您的webapp有两个不同的部分:服务器端和客户端,您可以使用适当的策略来模块化每个部分。 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. ASP.NET 5的前端包管理选项已经扩展。除了传统的NuGet包管理器之外,现在还支持Bower和NPM。 For example, consider how this article demonstrates installing jQuery via NPM . 例如,考虑一下本文如何演示如何通过NPM安装jQuery Here's another good article about setting up NPM, Bower, and Gulp in Visual Studio . 这是另一篇关于在Visual Studio中设置NPM,Bower和Gulp的好文章。

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. 怎么做:获取现有的客户端代码并制作自定义NPM或Bower包,然后使用来自一个或多个Asp.NET项目的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM