2012-10-06 39 views
9

我创建了默认的ASP.NET MVC 3 Web应用程序。然后,我添加了三个CSS和三个js文件到\查看\ Shared_Layout.cshtml观点:在ASP.NET MVC中组合和缩小JS和CSS

<!DOCTYPE html> 
<html> 
<head> 
    <title>@ViewBag.Title</title> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/StyleSheet1.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/StyleSheet2.css")" rel="stylesheet" type="text/css" /> 

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/JScript1.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/JScript2.js")" type="text/javascript"></script> 

</head> 
<body> 
    <div class="page"> 
     <div id="header"> 

....

当我运行的应用程序,我的HTML代码是

<!DOCTYPE html> 
<html> 
<head> 
    <title>Home Page</title> 
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> 
    <link href="/Content/StyleSheet1.css" rel="stylesheet" type="text/css" /> 
    <link href="/Content/StyleSheet2.css" rel="stylesheet" type="text/css" /> 

    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> 
    <script src="/Scripts/JScript1.js" type="text/javascript"></script> 
    <script src="/Scripts/JScript2.js" type="text/javascript"></script> 

</head> 
<body> 
    <div class="page"> 

是否有可能在MVC的处理程序,以改变我的HTML输出喜欢:

<!DOCTYPE html> 
    <html> 
    <head> 
     <title>Home Page</title> 
     <script src="js.axd=/Scripts/jquery-1.5.1.min.js,/Scripts/JScript1.js,/Scripts/JScript2.js" type="text/javascript"></script> 
     <link href="css.axd=/Content/Site.css,/Content/StyleSheet1.css,/Content/StyleSheet2.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body> 
     <div class="page"> 

所以链接js.axd=/Scripts/jquery-1.5.1.min.js,/Scripts/JScript1.js,/Scripts/JScript2.js 会将所有这些js文件的内容返回给浏览器,链接css.axd=/Content/Site.css,/Content/StyleSheet1.css,/Content/StyleSheet2.css将返回所有css文件的内容。

我之前在ASP.NET中做过一些IHttpHandler,但无法弄清楚如何在MVC中这样做,因为我只是MVC中的入门者。

任何帮助和代码示例将欣赏。 谢谢!

回答

0

我在自己的项目中使用cassette。此外,此列表here包含Nuget中的前20名。