前端腳本放在網站目錄裡,任何人打開開發者工具就能整段拿走。若這段程式碼是你產品價值的一部分,那麼把它編譯進 .NET 組件、以嵌入資源的形式隨 DLL 發行,是一個成本很低又能立刻收斂風險的做法。
以下示範怎麼做出一個「面板更新時邊框閃一下」的控制項,並把它的 JavaScript 藏進 DLL 裡。這個範例同時示範了 MS AJAX 的 ScriptManager 如何註冊嵌入資源。
先開一個類別庫專案,命名為 UpdateAnimate,然後加入三個參考:System.Web、System.Drawing、System.Web.Extensions。前兩個用來處理控制項與顏色,最後一個是 MS AJAX 的所在。
在專案裡新增一個 JScript 檔案 UpdatePanelAnimation.js,內容很單純:暫時改變 UpdatePanel 的邊框樣式,五百毫秒後收掉。
BorderAnimation = function(color)
{
this._color = color;
}
BorderAnimation.prototype =
{
animate: function(panelElement)
{
var s = panelElement.style;
s.borderWidth = '2px';
s.borderColor = this._color;
s.borderStyle = 'solid';
window.setTimeout(
function()
{
s.borderWidth = 0;
},
500);
}
}
在方案總管裡右鍵點選 UpdatePanelAnimation.js,檢視屬性,把「進階」底下的「產生操作」設為「嵌入的資源」。這一步是關鍵——少了它,檔案不會被放進 DLL,後面的註冊自然找不到東西。
新增一個類別 CustomControl,把程式碼替換成下面的內容。這個控制項對外暴露三個屬性:要作用在哪個 UpdatePanel(UpdatePanelID)、邊框顏色(BorderColor)、以及要不要啟用動畫(Animate)。真正的重頭戲在 OnPreRender 裡——它組出一段啟動腳本,透過 ScriptManager.RegisterStartupScript 註冊到頁面。
using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;
namespace UpdateAnimate
{
public class UpdatePanelAnimationWithClientResource : Control
{
private string _updatePanelID;
private Color _borderColor;
private Boolean _animate;
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; }
}
public string UpdatePanelID
{
get { return _updatePanelID; }
set { _updatePanelID = value; }
}
public Boolean Animate
{
get { return _animate; }
set { _animate = value; }
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Animate)
{
UpdatePanel updatePanel = (UpdatePanel)FindControl(UpdatePanelID);
string script = String.Format(
CultureInfo.InvariantCulture,
@"
Sys.Application.add_load(function(sender, args) {{
var {0}_borderAnimation = new BorderAnimation('{1}');
var panelElement = document.getElementById('{0}');
if (args.get_isPartialLoad()) {{
{0}_borderAnimation.animate(panelElement);
}}
}})
",
updatePanel.ClientID,
ColorTranslator.ToHtml(BorderColor));
ScriptManager.RegisterStartupScript(
this,
typeof(UpdatePanelAnimationWithClientResource),
ClientID,
script,
true);
}
}
}
}
值得留意的是 args.get_isPartialLoad() 這個判斷:它讓動畫只在「部分頁面更新」真正發生時觸發,一般的首次載入不會白白閃一下。這是 MS AJAX 生命週期裡常被忽略的一個小細節。
最後打開 AssemblyInfo.cs,加上一行,把虛擬名稱與 MIME 型別綁在一起:
[assembly: System.Web.UI.WebResource(
"UpdateAnimate.UpdatePanelAnimation.js",
"application/x-javascript")]
字串的路徑規則是「命名空間 + 點號 + 檔名」,大小寫必須和專案裡完全一致,否則執行期會找不到資源。完成後重新建置專案,DLL 就產生了。
接下來換到使用端。建立一個 AJAX-enabled 的網站專案,在根目錄下補一個 bin 資料夾,把控制項專案 bin\Debug(或 bin\Release)裡的 UpdateAnimate.dll 複製進去,然後改寫 Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="Samples"
Namespace="UpdateAnimate" Assembly="UpdateAnimate" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ScriptReference</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1"
EnablePartialRendering="True" runat="server">
<Scripts>
<asp:ScriptReference
Assembly="UpdateAnimate"
Name="UpdateAnimate.UpdatePanelAnimation.js" />
</Scripts>
</asp:ScriptManager>
<Samples:UpdatePanelAnimationWithClientResource
ID="UpdatePanelAnimator1"
BorderColor="Green"
Animate="true"
UpdatePanelID="UpdatePanel1"
runat="server">
</Samples:UpdatePanelAnimationWithClientResource>
<asp:UpdatePanel ID="UpdatePanel1"
UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Calendar ID="Calendar2" runat="server">
</asp:Calendar>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
執行後翻月曆,UpdatePanel 的邊框會短暫亮起綠色再消失。
ScriptReference 指向的是組件裡的資源名稱而非實體路徑,所以瀏覽器端只會看到一個由 ScriptResource.axd 輸出的壓縮內容,原始 js 檔案不會出現在網站目錄中。這確實提高了「隨手撿走」的門檻,也省下了部署時四散收管的麻煩,版本還能跟 DLL 一起控管。
但誠實地說,這不是加密。任何決心足夠的人,從瀏覽器的快取或開發者工具裡仍然拿得到執行中的程式碼。它的價值在於「提高取得成本」與「統一版本管理」,而不是絕對的保護。真正需要保密的邏輯,本來就不該放在前端——這條界線,任何前端混淆或嵌入技術都無法替你劃出來。