ASP.NET MVC:怎么组合脚本和其他资源

本文介绍了一种提高ASP.NET MVC应用性能的方法——使用MVCResourceCombine进行资源文件合并。通过将多个JavaScript和CSS文件合并为单一请求,显著减少了页面加载时的HTTP请求数量,从而提升了加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASP.NET MVC: How to combine scripts and other resources

 

ASP.NET pages that use AJAX components make usually many requests to server per one page to load all required JavaScript and CSS files. Connections, like all other real time resources, are most expensive to create and keep. If we can somehow decrease the number of requests per page load then we need less server resources for same amount of users. For ASP.NET forms we can use script combining, for ASP.NET MVC applications we can use ASP.NET MVC Client-side Resource Combine.

asp.net页面经常需要用ajax组件在服务器端请求许多javascript脚本和css文件.如果我们可以利用一些方法来减少请求的次数的话,相同的用户就可以访问更多的服务器资源啦.Asp.netForms 可以利用script组合,asp.net mvc程序同样可以用 ASP.NET MVC Client-side Resource Combine .

MVC Resource Combine is open-source project you can find in CodePlex. ASP.NET official ScriptManager needs server-side form as a container. This means that all scripts are loaded in the body of page. I have found no way how to make ScriptManager to create script and other references to page header. MVC Resource Combine is free of this problem.

ASP.NET MVC Client-side Resource Combine 是一个开源的项目可以再CodePlex上找到。asp.net官方的scriptManager需要一个服务器端的容器.也就是说必须把script加载到body里面,我没找到其他方法可以把script和其他资源放到header里面.MVC Resource Combine没这个问题.

MVC Resource Combine is easy to use. Just follow these steps.

  1. Download the latest release and add references to DLL-s of MVC Resource Combine.
  2. Add the following line to web.config file, under <configSections> element:
  3. MRC 用起来很简单,只需要几步.下载MVC Resources Combine 的dll文件,并添加下面的元素到web.config 的<configSections>节点

  4. <section name="resourceCombine" type="Mvc.ResourceCombine.ConfigSectionSetting, 
    Mvc.ResourceCombine" />

  5. Add the following line to web.config file, under <configuration> section:
    <resourceCombine definitionUrl="~/App_Data/combine.xml" />

      
  6. Now open Global.asax and add the following line before all the other route definitions:
    routes.AddResourceCombineRoute("Resource Combine");

  7. To App_Data folder add new XML-file combine.xml. By example, for jqGrid you may have JavaScript resource set like this:
    <?xml version="1.0" encoding="utf-8" ?>
    <resourceCombine url="~/combine.axd" defaultDuration="15" 
    defaultVersion="1" >
      <resourceSet name="jqGrid" type="js" duration="30" 
    version="a">
        <resource path="~/Scripts/jquery-1.3.2.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/jquery.jqGrid.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/js/jqModal.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/js/jqDnR.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/jquery.ajaxQueue.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/jquery.bgiframe.min.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/thickbox-compressed.js" 
    mode="LocalStatic" />
        <resource path="~/Scripts/jquery.autocomplete.js" 
    mode="LocalStatic" />
      </resourceSet>
    </resourceCombine>

  8. Now open Site.Master and add call for MVC Resource Combine and specify resource set by its name (jqGrid):  
    <%= Html.CombinerLink("jqGrid")%>

  9. Now run your application and view page source. You should see something like this:
    <script type="text/javascript" src="/combine.axd/jqGrid/a">
    </script>

NB! Don’t forget to add reference to Mvc.ResourceCombine namespace in your Global.asax and Site.Master files. Well, if you have Resharper, then it is almost possible to forget these references. :)

Now, let’s see results. It’s Saturday and I’m watching Estonian biggest song contest, so I’m too lazy to add Firebug screenshots here. I have a little bit different configuration but numbers in the following table should give you some idea about wins in performance.

Before combine After ombine Difference (before/after)

Requests 21  4 5.25

Size (kb) 259 19 13.63

Time (s)7.69 4.84 1.59

As you can see, using MVC Resource Combining it is possible to achieve better performance with pretty simple and time consuming efforts. Also you don’t have to mix ScriptManager and other ASP.NET forms elements to your ASP.NET MVC views. By the way, you can combine also all the other resources that can be downloaded as one file – by example, style sheets.

你可以看到,用mvc resurce combining 具有更好的执行效率.你也不需要在mvc 视图里面混合ScriptManage或者其他asp.net forms的元素啦.另外,你也可以组合其他可以下载的资源比如样式文件.

转载于:https://www.cnblogs.com/doublog/archive/2009/07/21/1527844.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值