Asp.net 將js檔案打包進dll 方法 收藏
註:本文基於.NET 2.0 和 VS2005
我們在編寫 Server Control 的時候難免要用到一些用戶端指令碼(javascript),如何把指令碼和編譯好的dll一起發布就成了一個問題。把一段一段的javascript block寫在cs檔案裡是一件很“醜陋”的事情,javascript就應呆在*.js檔案裡。js檔案怎樣才能“打包”到dll裡呢?查了很多文檔,最後實踐下來發現有很多細節是需要注意的。整理出來,免得大家走彎路。廢話無多,讓我們開始。
Step 0: 我們已有的
1. 網站項目:Website1 ,其中:
Default.aspx (空頁面)
2. WebControl庫項目:WebControlLibrary1 ,其中:
ClientScriptResourceLabel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
public class ClientScriptResourceLabel : WebControl
{
}
}
script_include.js
function DoClick() {Form1.Message.value='Text from resource script.'}
Step 1:
在script_include.js檔案的屬性視窗裡,把Build Action改為:Embedded Resource
Step 2:
在ClientScriptResourceLabel.cs 中加入
[assembly: WebResource("script_include.js", "application/x-javascript")]
namespace WebControlLibrary1
{
....
注意這句是在namespace之外。你也可以把這句加在AssemblyInfo.cs檔案裡,.NET的類庫就是統一加在AssemblyInfo.cs檔案裡的。
很多文檔(包括MSDN)裡都說通過以上兩步就可以把js檔案作為資源,捆綁到dll中了。但實際上更不就不能用。我們用Reflector來看看編譯出來的到底是什麼東東。
怎麼變成 WebControlLibrary1.script_include.js 了?!問題是出在“預設名稱空間”。VS會自動把default namespace加到資源檔前面。其實只要把預設名稱空間該為空白就可以了。令人鬱悶的是VS2005正式版不允許把default namespace改為空白(beta2是允許的)。怎麼辦呢?難道要用麻煩的命令列來編譯嗎?還有一個辦法就是手工修改專案檔。
Step 3:
用寫字板開啟WebControlLibrary1.csproj檔案,把其中的RootNamespace 改為空白
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{65431F13-ABAE-4281-A860-90FEC739AFED}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace></RootNamespace>
<AssemblyName>WebControlLibrary1.web</AssemblyName>
</PropertyGroup>
這樣一來“預設名稱空間”就沒有了:
Step 4:
編譯WebControlLibrary1,這下得到的是我們想要的了:
Step 5 :
呼叫指令碼資源(ClientScriptResourceLable.cs)
public class ClientScriptResourceLabel : WebControl
{
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
ClientScriptManager manager1 = this.Page.ClientScript;
manager1.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "script_include.js");
}
base.OnPreRender(e);
}
}
Step 6 :
終於可以在頁面裡使用封裝好的控制項了(Default.aspx):
<%@ Page Language="C#" %>
<%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" TagPrefix="cc1" %>
<html>
<head runat="server">
<title>Script Resource</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text" id="Message">
<input type="button" onclick="DoClick()" value="ClientClick">
<cc1:ClientScriptResourceLabel ID="ClientScriptResourceLabel1" runat="server" />
</div>
</form>
</body>
</html>
產生的頁面是這樣的:
<html>
<head><title>
Script Resource
</title></head>
<body>
<form name="Form1" method="post" action="Default.aspx" id="Form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkwOTU4NDc0OGRkO0UjKICXV1XisDv/KKM/wA+05FQ=" />
</div>
<script src="/WebSite1/WebResource.axd?d=E2u_4K_tSvgEe7jglgaDJYjGQkJj2ZwZEqAWVi3afWYe4CI30IeNjer7_ojoLKjr0&t=632688246616562500" type="text/javascript"></script>
<div>
<input type="text" id="Message">
<input type="button" onclick="DoClick()" value="ClientClick">
<span id="ClientScriptResourceLabel1"></span>
</div>
</form>
</body>
</html>
其中的<script src="/WebSite1/WebResource.axd?d=...... 就是對指令碼資源的調用。
注意:除了default namespace會影響編譯出來的指令碼資源檔名外,檔案所在的位置也會作為首碼加到檔案名稱上。例如你把script_include.js放到 JS 目錄下,編譯出來就會變成 JS.scritp_include.js