添加新的ASP.NET Web优化框架MVC4项目后手动升级它们 [英] Adding the new ASP.NET Web Optimization framework to MVC4 projects after manually upgrading them
问题描述
在使用这些指令,怎么办手动升级ASP.NET MVC项目MVC4 您然后设置的ASP.NET Web优化框架中MVC4的新的CSS和JavaScript的资产捆绑和最小化功能?默认模板有这一切成立,但你怎么用手工做的?
After manually upgrading an ASP.NET MVC project to MVC4 using these instructions, how do you then set up the new CSS and JavaScript asset bundling and minimization features of the ASP.NET Web Optimization Framework in MVC4? The default templates have this all set up, but how do you do it by hand?
推荐答案
- 右键单击引用,然后管理的NuGet包,并添加Microsoft.AspNet.Web.Optimization(或键入
安装封装Microsoft.AspNet.Web.Optimization
进入的NuGet控制台)。 - 在Web.config文件,添加以下
< system.webServer>
,允许缩小的捆绑与扩展名的URL担任 。
- Right click References then Manage NuGet Packages and add "Microsoft.AspNet.Web.Optimization" (or type
Install-Package Microsoft.AspNet.Web.Optimization
into the NuGet console). - In your Web.config file, add the following to
<system.webServer>
, allowing the minified bundles to be served with extensionless URLs.
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
- 在App_Start文件夹,添加一个新类叫BundleConfig.cs。它应该是这个样子:
using System.Web;
using System.Web.Optimization;
namespace MvcApplication1
{
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css"));
}
}
}
- 编辑上面添加脚本和样式表包您需要再添加以下行使用部分,并在的Application_Start Global.asax.cs中:
//using section
using System.Web.Optimization;
//Application_Start
BundleConfig.RegisterBundles(BundleTable.Bundles);
- 用@ Styles.Render调用
替换您在_Layout.cshtml CSS和JavaScript和标签(〜/内容/ CSS)
和@ Scripts.Render(〜/包/ jQuery的)
,与您加入到BundleConfig.cs束的名称替换参数。确保不指定任何束一样在你的项目文件夹。 - Replace your CSS and JavaScript and tags in _Layout.cshtml with calls to
@Styles.Render("~/Content/css")
and@Scripts.Render("~/bundles/jquery")
, replacing the parameters with the names of the bundles you added to BundleConfig.cs. Make sure not to name any of the bundles the same as folders in your project.
您现在应该准备就绪 - 如何在这里使用完整功能集念起来:<一href=\"http://www.asp.net/mvc/overview/performance/bundling-and-minification\">http://www.asp.net/mvc/overview/performance/bundling-and-minification
You should now be all set – read up on how to use the full featureset here: http://www.asp.net/mvc/overview/performance/bundling-and-minification
这篇关于添加新的ASP.NET Web优化框架MVC4项目后手动升级它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!