Knowledge points used in MVC project development (mvccontrib separates ASP. net mvc Project)

Source: Internet
Author: User

Preface

The use of areas in javasmvc3.0. In this project, mvccontrib is used to separate projects in later stages of development, such as the simplest separation of WEB projects, front-end and back-end.

Mvccontrib 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.

 

Question

1. Create an ASP. NET mvc3.0 project myportable (foreground project), select Internet application as the project template, and select razor as the view engine.

2. Then add another ASP. NET mvc3.0 project myportable. Admin (background project) to the solution. Select an empty project as the project template, and select razor as the view engine.

3. Right-click the myportable. 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 });}}

4. You can use nuget to install mvccontrib for myportable. Admin:

You can also download http://mvccontrib.codeplex.com/on the official website. Download the decompressed file as follows:

The mvccontrib. dll file can be referenced under myportable. admin.

5. Open adminarearegistration. CS, change its base class arearegistration to portablearearegistration, and modify the registerarea method declaration:

Public override void registerarea (arearegistrationcontext context, iapplicationbus)

Add the namespace restriction to the default route and add registerareaembeddedresources () to the registerarea method (). It seems like this:

 
Public override void registerarea (arearegistrationcontext context, iapplicationbus Bus) {context. maproute ("admin_default", "admin/{controller}/{action}/{ID}", new {Action = "Index", id = urlparameter. optional}, new string [] {"myportable. admin. controllers "});}

And modify it in the global. asax. CS file of the project.

Public static void registerroutes (routecollection routes) {routes. ignoreroute ("{resource }. axd/{* pathinfo} "); routes. maproute ("default", // route name "{controller}/{action}/{ID}", // URL with parameters new {controller = "home ", action = "Index", id = urlparameter. optional}, // default value of the parameter New String [] {"myportable. controllers "});}

6. Add a homecontroller and index action under myportable. admin/controllers and add the corresponding View File.

 
Namespace myportable. admin. controllers {public class homecontroller: controller {// get:/home/Public actionresult index () {return view ();}}}

7. Select "embedded resources" for all CSS, JS, and image static file attributes-build action )".

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.

8. reference the admin project in the main project. Add a folder named areas to the main project and copy myportable/views/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.

9. Compile and run the command to check the effect.

You can also access static files. Add the corresponding routes to the adminarearegistration. CS file.

 
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.5.1.min.js to access jquery. Note that "." is in the middle of "scripts. jquery-1.5.1.min.js" instead of "/".

In view, you can also use the access form of <% = URL. Resource ("scripts. jquery-1.5.1.min.js") %>. The URL. Resource () method is integrated into mvccontrib.

In this way, we can fully controlProgramAll static embedded Resources in the set. If you want to access embedded resources by accessing physical files, you can add the following routes:

 
// Scriptscontext. maproute (areaname + "_ scripts", base. arearouteprefix + "/scripts/{resourcename}", new {controller = "embeddedresource", Action = "Index", resourcepath = "scripts"}, new [] {"mvccontrib. portableareas "}); // contentcontext. maproute (areaname + "_ content", base. arearouteprefix + "/content/{resourcename}", new {controller = "embeddedresource", Action = "Index", resourcepath = "content"}, new [] {"mvccontrib. portableareas "});

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:

    1. 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.
    2. 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 ).

ExampleCodeHttp://files.cnblogs.com/aehyok/MvcContrib.rar

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.