現在有一個發佈好的WebService,地址是http://hovertree.com:2706/UploadExpenseToConstract.asmx能不能把最後面的asmx去掉呢?變成http://hovertree.com:2706/UploadExpenseToConstract但效果和 ...
現在有一個發佈好的WebService,地址是http://hovertree.com:2706/UploadExpenseToConstract.asmx
能不能把最後面的asmx去掉呢?變成
http://hovertree.com:2706/UploadExpenseToConstract
但效果和原來的是一樣的?
具體代碼:
Global.asax
using System; namespace ExampleHoverTree { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { string hPath= Request.Url.LocalPath.ToString().ToLower(); if (!hPath.Contains("/hovertreewm.asmx")) { if (hPath.Contains("/hovertreewm")) { Context.RewritePath(hPath.Replace("/hovertreewm", "/hovertreewm.asmx")); } } } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
HoverTreeWM.asmx
using System.Web.Services; namespace ExampleHoverTree.HtExample.HWebMethod { /// <summary> /// HoverTreeWM 的摘要說明 /// </summary> [WebService(Namespace = "http://hovertree.top/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消註釋以下行。 by 何問起 [System.Web.Script.Services.ScriptService] public class HoverTreeWM : System.Web.Services.WebService { [WebMethod] public string HLogin(string hUsername,string hPassword) { // return hUsername + hPassword; return (hUsername.ToLower() == "hewenqi" && hPassword == "HoverTree") ? "登錄成功" : "用戶名或密碼錯誤"; } } }
HoverTreeAjax.htm
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>asp.net WebService去掉asmx尾碼示例_何問起</title> <script src="http://down.hovertree.com/jquery/jquery-1.12.4.min.js"></script> <style type="text/css"> .hovertreelogin { height: 400px; display: flex; justify-content: center; align-items: center; text-align: center; } </style> </head> <body> <div class="hovertreelogin"> <table> <tr><td>用戶名:</td><td><input type="text" id="husername" /></td><td></td></tr> <tr><td>密 碼:</td><td><input type="password" id="hpassword" /></td><td></td></tr> <tr><td></td><td><input type="button" value="登錄" id="hlogin" /></td><td></td></tr> <tr><td></td><td><br />正確用戶名為:hewenqi<br />密碼為HoverTree</td><td></td></tr> </table> </div> <script> $(function(){ $(function () { $("#hlogin").on("click", function () { $.ajax({ type: 'Post', url: '/HtExample/HWebMethod/hovertreeWM/HLogin', contentType: "application/json; charset=utf-8", dataType: "json", data: "{'hUsername':'" + $("#husername").val() + "','hPassword':'" + $("#hpassword").val() + "'}", success: function (data, textStatus) { alert(data.d); }, error: function (xmlHttpRequest, textStatus, errorThrown) { alert("err " + errorThrown); } }); }) }) }) </script> </body> </html>
效果圖:
源碼下載:http://hovertree.com/h/bjaf/hv6cqe5n.htm
推薦:http://www.cnblogs.com/sosoft/p/csharpemail.html