1. 建立一個項目,在引用中添加引用Ajax.dll,Ajax.dll位於下載的壓縮包裡面。
2.建立HttpHandler,在web.config裡面加上 <configuration>
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
...
<system.web>
</configuration>
3.建立一個類DemoMethods,這個類實現擷取用戶端MAC地址: using System;
using System.Web;
namespace AjaxSample
{
///
/// Summary description for Methods.
///
public class DemoMethods
{
[Ajax.AjaxMethod]
public string GetCustomerMac(string clientIP) //para IP is the client's IP
{
string mac = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "nbtstat";
process.StartInfo.Arguments = "-a "+clientIP;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
int length = output.IndexOf("MAC Address = ");
if(length> 0)
{
mac = output.Substring(length+14, 17);
}
process.WaitForExit();
return mac.Replace("-", "").Trim();
}
}
}
4.寫javascript,建立一個名為default,js檔案如下:
function GetMac()
{
var clientIP="192.168.0.1";
document.getElementById("Mac").value=DemoMethods.GetCustomerMac(clientIP).value
alert(DemoMethods.GetCustomerMac(clientIP).value);
}
5.在某個Aspx頁面放上一個html 的button
在頁面上 中引用default.js :
在INPUT的onclick事件中加上onclick="javascript:GetMac()"
value="用戶端擷取IP" onclick="javascript:GetMac();">
6.修改Global.asax的Application_Start事件,設定Ajax的HandlerPath :
protected void Application_Start(Object sender, EventArgs e)
{
Ajax.Utility.HandlerPath = "ajax";
}
需要注意的是:該版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath = "ajax"; 設定檔web.config必須加上HttpHandler的配置資訊!