Overview
In the development of ASP. net mvc complex projects, as the project scale expands, we may need to separate different modules as needed. You can use the "Areas" function provided by the ASP. net mvc Framework to organize projects. For details, see use Areas to separate ASP. net mvc projects. However, from the above, we can see that several projects are centered around Areas, and the "Build Event" is finally integrated together, and there is always a feeling of "broken connections. It can be said that the project independent from Areas has not been completely separated. In the above comments, some friends pointed out that MvcContrib can be used, so they learned a bit.
MvcContrib (Portable Area) can compile all the content (including Views, Controllers, Scripts, etc.) in an MVC project into a dll. In this way, the MVC project can be used as a "plug-in (or component)/Plugin (or Widget)" for other projects and is highly reusable.
Environment preparation
Still consider the scenario above: Separate the user-oriented foreground from the Administrator background.
- Create an ASP. NET MVC3 project MyPortableAreaDemo (foreground project), select "Internet Application" as the project template, and select "Razor" as the view engine ".
- Add an empty MVC project MyPortableAreaDemo. Admin (background project) and delete Global. asax.
- Right-click the MyPortableAreaDemo. Admin project, add a class AdminAreaRegistration. cs, and enter the following content:
public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } }
Install MvcContrib
Use NuGet to install MvcContrib for MyPortableAreaDemo. Admin:
Alternatively, enter Install-Package MvcContrib In the Package Manager Console to Install the Package.
Use MvcContrib
Open AdminAreaRegistration. cs, change its base class AreaRegistration to PortableAreaRegistration, and change the RegisterArea method declaration to (important !) :
public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
Add a namespace limit to the default route and add RegisterAreaEmbeddedResources () to the RegisterArea method (). It seems like this:
Add a HomeController and Index Action under MyPortableAreaDemo. Admin/Controllers and add the corresponding View File:
The following step is very important:
Select "Embedded Resources" for the "Build Action" attribute of all css, js, and image static files )". This means that all these static files will be compiled into the dll file, instead of being a separate physical file as before. The advantage of this is that the structure of the entire project is relatively fixed, and a dll includes all the content of the entire project, which is highly reusable. The disadvantage is that every time you modify the content of this project (even if you modify static content such as js or view), you must recompile the entire project. This depends on your choice. You can weigh whether it is worthwhile to do so.
Now, the Admin project is referenced in the main project. Add a folder named Areas in the main project and copy MyPortableAreaDemo/Web. config to the newly created Areas folder.
Why? This is because the Portable Area in the Admin project will be mapped to this Areas folder when it is loaded by the main project, in this case, the Controller searches for the corresponding Views under Areas (instead of in the main project.
All right, compile now (Remember, you must recompile all the modifications to the Portable Areas Project !) The whole solution is accessed after the main project is started:/Admin/Home/Index. If everything goes well, you will see:
Access static files
As we mentioned above, all static files (js, css, and image) are compiled into dll. How can we access these static content? Try accessing the/Admin/Scripts/jquery-1.4.4.min.js directly, and the browser prompts "cannot find resource ". Therefore, we need to modify AdminAreaRegistration. cs and add the following route:
context.MapRoute( "ResourceRoute", base.AreaRoutePrefix + "/resource/{resourceName}", new { controller = "EmbeddedResource", action = "Index" }, new[] { "MvcContrib.PortableAreas" } );
This route means that all the static resources are handed over to MvcContrib. PortableAreas. EmbeddedResource for processing, so now we can use:/Admin/resource/Scripts. jquery-1.4.4.min.js to access jquery. Note that "." is in the middle of "Scripts. jquery-1.4.4.min.js" instead of "/".
In View, you can also use the access form of <% = Url. Resource ("Scripts. jquery-1.4.4.min.js") %>. The Url. Resource () method is integrated into MvcContrib.
In this way, we can completely control all static embedded Resources in the Assembly. To access embedded resources by accessing physical files, you can add the following route:
//Scripts context.MapRoute( AreaName + "_Scripts", base.AreaRoutePrefix + "/Scripts/{resourceName}", new { controller = "EmbeddedResource", action = "Index", resourcePath="Scripts" }, new[] { "MvcContrib.PortableAreas" } ); //Content context.MapRoute( AreaName + "_Content", base.AreaRoutePrefix + "/Content/{resourceName}", new { controller = "EmbeddedResource", action = "Index", resourcePath = "Content" }, new[] { "MvcContrib.PortableAreas" } );
Note the value of resourcePath. Now we can access embedded resources directly using/Admin/Scripts/jquery-1.4.4.min.js.
Summary
Through MvcContrib Portable Area, we can effectively separate the MVC project and compile the entire isolated project into a dll using embedded resources. You can copy and reference the project at will, good reusability.
However, this method also has the following shortcomings:
- Because all static resources are compiled into the dll, it is inevitable that the dll volume will become larger and larger, especially when there are many images.
- Static resource access. If there are sub-folders under the Content and Scripts folders above (this is a common case), you can only access them through resource instead of pseudo physical addresses, which is not too friendly.
Based on the above two points, we recommend that you only compile the view file into the dll as embedded resources. All static files (js, css, and image) can be placed in the main project for direct access. You can also put it in the Admin project and synchronize it to the corresponding directory of the main project by using Build Event (refer to the above ).
Next, I want to study the nopCommerce project separation method and plug-in development, hoping to make improvements every day.
Project source code download: http://files.cnblogs.com/dingji/MyPortableAreaDemo.zip