ASP. net mvc 4.0 casual record

Source: Internet
Author: User

Use System. Web. Optimization to operate scripts and Style Sheets

This is also the focus of this chapter. First, we can use vs2012rc to create a new mvc4.0 project. The version can be 4.0 or 4.5. In global. in the asax file code, we found that the filter, router, and bundles of the style sheet and script have been moved to other pages, that is, in the app_start folder under the root directory. The code is just a few simple lines. The Code is as follows:

1 using system;
2 using system. Collections. Generic;
3 using system. configuration;
4 using system. Data. entity;
5 using system. Data. entity. Infrastructure;
6 using system. LINQ;
7 using system. Web;
8 using system. Web. HTTP;
9 using system. Web. MVC;
10 using system. Web. optimization;
11 using system. Web. Routing;
12
13 namespace mvc4
14 {
15 // Note: For instructions on enabling IIS6 or iis7 Classic mode,
16 // visit http://go.microsoft.com /? Linkid = 9394801
17
18 public class mvcapplication: system. Web. httpapplication
19 {
20 protected void application_start ()
21 {
22 arearegistration. registerallareas ();
23
24 filterconfig. registerglobalfilters (globalfilters. filters );
25 routeconfig. registerroutes (routetable. routes );
26 bundleconfig. registerbundles (bundletable. bundles );
27}
28}
29}

Three files are added to the app_start directory, including rilterconfig. CS, routeconfig. CS, and bundleconfig. CS. In the beta version, they are still directly written in the global. asax file, and their respective functions can be known from the name. Today we mainly learn about the content and functions of the bundleconfig. CS file.

 

The bundleconfig. CS file contains the script and style table file paths used in some applications. Wildcards can be used. The Code is as follows:

1 using system. Web;
2 using system. Web. optimization;
3
4 namespace mvc4
5 {
6. Public class bundleconfig
7 {
8 public static void registerbundles (bundlecollection bundles)
9 {
10 bundles. Add (New scriptbundle ("~ /Bundles/jquery "). Include (
11 "~ /Scripts/jquery-1 .*"));
12
13 bundles. Add (New scriptbundle ("~ /Bundles/jqueryui "). Include (
14 "~ /Scripts/jquery-UI *"));
15
16 bundles. Add (New scriptbundle ("~ /Bundles/jqueryval "). Include (
17 "~ /Scripts/jquery. unobtrusive *",
18 "~ /Scripts/jquery. Validate *"));
19
20 bundles. Add (New scriptbundle ("~ /Bundles/modernizr "). Include (
21 "~ /Scripts/modernizr -*"));
22
23 bundles. Add (New stylebundle ("~ /Content/CSS "). Include ("~ /Content/site.css "));
24
25 bundles. Add (New stylebundle ("~ /Content/themes/base/CSS "). Include (
26 "~ /Content/themes/base/jquery.ui.core.css ",
27 "~ /Content/themes/base/jquery.ui.resizable.css ",
28 "~ /Content/themes/base/jquery.ui.selectable.css ",
29 "~ /Content/themes/base/jquery.ui.accordion.css ",
30 "~ /Content/themes/base/jquery.ui.autocomplete.css ",
31 "~ /Content/themes/base/jquery.ui.button.css ",
32 "~ /Content/themes/base/jquery.ui.dialog.css ",
33 "~ /Content/themes/base/jquery.ui.slider.css ",
34 "~ /Content/themes/base/jquery.ui.tabs.css ",
35 "~ /Content/themes/base/jquery.ui.datepicker.css ",
36 "~ /Content/themes/base/jquery.ui.progressbar.css ",
37 "~ /Content/themes/base/jquery.ui.theme.css "));
38}
39}
40}

These are all about bundle applications. From the code, we can see that the bundle Instance Object (script and style) contains a virtual directory, this directory can be used as a unique key in the page. Of course, during optimization, this virtual directory will be displayed on the front-end page, which will be further described later. Next let's take a look at how to use them in the Code, in the system. web. the optimization program contains two classes: scripts and styles, which are used to present the scripts and style sheets in the bundle set respectively. The Code is as follows:

1 @ styles. Render ("~ /Content/themes/base/CSS ","~ /Content/CSS ")
2 @ scripts. Render ("~ /Bundles/modernizr ")

We can see from the above that two bundled style sheets and a script are introduced to the page, which include all the files in the include method, debug the HTML code at the front end. Another method in them is the introduction of external files, such as files in CDN, such as API files such as Google.

 

Iv. style sheet and script Optimization

This is actually included in the third part of the article. I come out independently. The main feeling is that I think he is a good guy. The code is simple, but it is very suitable. I don't know if you have used ajaxminify. You can compile the script and compress it into the smallest content. However, all commands are required. However, in mvc4.0, system. web. optimization already contains this stuff. They are jsminify and cssminify. Don't underestimate these two classes, although there are only two public methods.

The above method is to use scripts and styles to introduce scripts and style sheets to the page, and compress the script and style sheet compilation and input to the client without having to modify any code, this not only effectively increases the difficulty of jshack and reduces the file size. To achieve this goal, we only need to set an attribute in bundletable to true. The Code is as follows:

1 using system;
2 using system. Collections. Generic;
3 using system. configuration;
4 using system. Data. entity;
5 using system. Data. entity. Infrastructure;
6 using system. LINQ;
7 using system. Web;
8 using system. Web. HTTP;
9 using system. Web. MVC;
10 using system. Web. optimization;
11 using system. Web. Routing;
12
13 namespace mvc4
14 {
15 // Note: For instructions on enabling IIS6 or iis7 Classic mode,
16 // visit http://go.microsoft.com /? Linkid = 9394801
17
18 public class mvcapplication: system. Web. httpapplication
19 {
20 protected void application_start ()
21 {
22 arearegistration. registerallareas ();
23
24 filterconfig. registerglobalfilters (globalfilters. filters );
25 routeconfig. registerroutes (routetable. routes );
26 bundletable. enableoptimizations = true;
27 bundleconfig. registerbundles (bundletable. bundles );
28}
29}
30}

Is it amazing that the red font above is the line of code we want to add. About ASP. net 4.5 MVC 4.0 is worth exploring. Because I am not a professional academic researcher and I am not well-organized enough to write articles, I think about where they are, there is no time to regulate the writing from easy to difficult or something, and I am sorry for the bad browsing effect.

Resource file filtering rules

The background tracked and found that files with the suffix .intelliisense.js0000-vsdoc.js0000.debug.js0000.min.js0000.min.css were filtered out.

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.