.NET Ajax的無重新整理技術執行個體詳解

來源:互聯網
上載者:User
ajax|重新整理|無重新整理|詳解 下載一個.net Ajax開發包,該開發包包括ASP2.0和目前ASP1.1版使用的Ajax,詳細地址參見http://ajax.schwarz-interactive.de/,接下來,開始。

  1. 建立一個項目,在引用中添加引用Ajax.dll,Ajax.dll位於下載的壓縮包裡面。

  2.建立HttpHandler,在web.config裡面加上:

<configuration>lt;system.web>lt;httpHandlers>lt;add verb="POST,GET" path="ajax/*.ashx"type="Ajax.PageHandlerFactory,Ajax" />lt;/httpHandlers> <system.web>lt;/configuration>

  3.建立一個類DemoMethods,這個類實現擷取用戶端MAC地址:

using System;using System.Web;namespace AjaxSample{/**//// <summary>/// Summary description for Methods./// </summary>ublic class DemoMethods{[Ajax.AjaxMethod]ublic string GetCustomerMac(string clientIP) //這裡輸入用戶端IP,這個函數知識測試用,你也可以寫一個其他的簡單一點的函數代替{ tring mac = "";System.Diagnostics.Process process =new System.Diagnostics.Process();rocess.StartInfo.FileName = "nbtstat";rocess.StartInfo.Arguments = "-a "+clientIP;rocess.StartInfo.UseShellExecute = false;rocess.StartInfo.CreateNoWindow = true;rocess.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).valuealert(DemoMethods.GetCustomerMac(clientIP).value);}

  5.在某個Aspx頁面放上一個html 的button在頁面上中引用default.js :

lt;script language="javascript"src="default.js"></script>

  在INPUT的onclick事件中加上

lt;INPUT style="Z-INDEX: 101; LEFT: 392px; POSITION: absolute;TOP: 176px" type="button"

  value="用戶端擷取IP" >

  6.在page頁面的Page_Load事件中加上

private void Page_Load(object sender, System.EventArgs e){// 在此處放置使用者代碼以初始化頁面Ajax.Utility.RegisterTypeForAjax(typeof(AjaxSample.DemoMethods));}

  注意:typeof(AjaxSample.DemoMethods)中,AjaxSample是命名空間,DemoMethods是要包含要調用方法的類,即上面第3步.建立類DemoMethods

7.修改Global.asax的Application_Start事件,設定Ajax的HandlerPath :

protected void Application_Start(Object sender, EventArgs e){Ajax.Utility.HandlerPath = "ajax";}


  運行看看效果。是不是沒有重新整理就在伺服器端取到用戶端的MAC地址?

  需要注意的是:該版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath = "ajax"; 設定檔web.config必須加上HttpHandler的配置資訊!

  該開發包的新版本還沒有來得及體驗,估計新版本中會方便一些,可能會去掉手動的設定Global.asax的Application_Start事件中加上Ajax.Utility.HandlerPath = "ajax";以及其他麻煩的設定!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.