1. Create a new project and add reference Ajax. DLL to the reference. Ajax. dll is located in the downloaded package.
2. Create httphandler and add it to Web. config.<configuration>
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
...
<system.web>
</configuration>
3. Create a new class demomethods, which can obtain the client MAC address: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. Write JavaScript and create a new file named default. The JS file is as follows:
function GetMac()
{
var clientIP="192.168.0.1";
document.getElementById("Mac").value=DemoMethods.GetCustomerMac(clientIP).value
alert(DemoMethods.GetCustomerMac(clientIP).value);
}
5. Put an HTML button on An ASPX page
Reference default. js on the page:
Add onclick = "javascript: getmac ()" to the onclick event of input ()"
Value = "Client IP" onclick = "javascript: getmac ();">
6. Modify the application_start event of Global. asax and set the Ajax handlerpath:
Protected void application_start (Object sender, eventargs E)
{
Ajax. Utility. handlerpath = "ajax ";
}
Note that the. NET Ajax version must be manually added with Ajax. Utility. handlerpath = "ajax" in global. asax. The httphandler configuration information must be added to the web. config configuration file!