2012-09-30 108 views
1

我試圖將傳入的URL從ajax請求映射到Global.asax中的Web服務方法。 Web服務位於此路徑/ajax/BookWebService.asmx,它有2個方法GetListOfBookGetListOfAuthor重寫ASMX Web服務的URL

我想在腳本標記中使用url:/ajax/book/list而不是url:/ajax/BookWebService.asmx/GetListOfBook

這裏是我的腳本和標記:

<script type="text/javascript"> 

    $(function() { 
     $("#btnCall").click(function() { 

      $.ajax({type: "POST", 
       url: "/ajax/book/list", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (response) { 
        var list = response.d; 
         $('#callResult').html(list); 
       }, 
       error: function (msg) { 
        alert("Oops, something went wrong"); 
       } 
      }); 

     }); 
    }); 

</script> 

<div> 
    <div id="callResult"></div> 
    <input id="btnCall" type="button" value="Call Ajax" /> 
</div> 

這裏是在Global.asax:

void Application_BeginRequest(object sender, EventArgs e) 
{ 
    var currentRequest = HttpContext.Current.Request.Url.AbsolutePath; 

    if (currentRequest.Contains("ajax/book/list")) { 
     HttpContext.Current.RewritePath("/ajax/BookWebService.asmx/GetListOfBook"); 
    } 

} 

當我在Firebug控制檯檢查,這是我所得到的:

"NetworkError: 500 Internal Server Error - localhost:13750/ajax/book/list"

如果我將url:/ajax/book/list更改爲url:/ajax/BookWebService.asmx/GetListOfBook它按預期工作。

我使用VS 2010和.NET 4

我怎麼做才能/ajax/book/list而不是/ajax/BookWebService.asmx/GetListOfBook Ajax調用?

+0

Web API是不是更適合該任務的工具?您可以使用javascript將數據發佈到Web API方法,默認情況下它將以JSON形式返回(使用內容類型)。同樣的XML等...一個例子http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api – Mike

+0

是的,確實。但是我有一個使用ASMX構建的小型項目,它需要一種方法來保持乾淨的回調URL。 – Narazana

+0

如果它的一個小項目,不應該很難轉換:P – Mike

回答

2

如果你可以改變Ajax調用鍵入: 「GET」,試試這個變化:

Ajax調用:

$(function() { 
     $("#btnCall").click(function() { 

      $.ajax({ type: "GET", //Change 1 
       url: "/ajax/book/list", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (response) { 
        var list = response.d; 
        $('#callResult').html(list); 
       }, 
       error: function (msg) { 
        alert("Oops, something went wrong"); 
       } 
      }); 

     }); 
    }); 

</script> 

的Global.asax:

void Application_BeginRequest(object sender, EventArgs e) 
    { 
     var currentRequest = HttpContext.Current.Request.Url.AbsolutePath; 
     //Change 2 
     if (currentRequest.Contains("ajax/book/")) // maybe you can use a generic path to all rewrites... 
     { 
      IgnoreWebServiceCall(HttpContext.Current); 
      return; 
     } 
    } 
    //Change 3 
    private void IgnoreWebServiceCall(HttpContext context) 
    { 
     string svcPath = "/ajax/BookWebService.asmx"; 

     var currentRequest = HttpContext.Current.Request.Url.AbsolutePath; 

     //You can use a switch or.... 
     if (currentRequest.Contains("ajax/book/list")) 
     { 
      svcPath += "/list"; 
     } 

     int dotasmx = svcPath.IndexOf(".asmx"); 
     string path = svcPath.Substring(0, dotasmx + 5); 
     string pathInfo = svcPath.Substring(dotasmx + 5); 
     context.RewritePath(path, pathInfo, context.Request.Url.Query); 
    } 

BookWebService。 asmx:

[WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [ScriptService] ///******************* Important 
    public class BookWebService : System.Web.Services.WebService 
    { 

     [WebMethod] 

     [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] ///******************* Important 
     public string list() 
     { 
      return "Hi"; 

     } 

    } 

適用於我:)

+0

是的,它的工作原理。我只將IgnoreWebServiceCall添加到Global.asax。其他一切都保持不變。謝謝。 – Narazana