Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重写ASMX Web服务的URL_C#_Url Rewriting_Asmx_Global Asax - Fatal编程技术网

C# 重写ASMX Web服务的URL

C# 重写ASMX Web服务的URL,c#,url-rewriting,asmx,global-asax,C#,Url Rewriting,Asmx,Global Asax,我正在尝试将传入的URL从ajax请求映射到Global.asax中的web服务方法。 web服务位于路径/ajax/BookWebService.asmx,它有两种方法GetListOfBook和GetListOfAuthor 我想在脚本标记中使用url:/ajax/book/list而不是url:/ajax/BookWebService.asmx/GetListOfBook 以下是我的脚本和标记: <script type="text/javascript"> $(func

我正在尝试将传入的URL从ajax请求映射到Global.asax中的web服务方法。 web服务位于路径
/ajax/BookWebService.asmx
,它有两种方法
GetListOfBook
GetListOfAuthor

我想在脚本标记中使用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>
当我检查FireBug控制台时,我得到的是:

“NetworkError:500内部服务器错误-本地主机:13750/ajax/book/list”

如果我将url:
/ajax/book/list
更改为url:
/ajax/BookWebService.asmx/GetListOfBook
,它将按预期工作

我正在使用VS2010和.NET4


如何对
/ajax/book/list
进行ajax调用,而不是
/ajax/BookWebService.asmx/GetListOfBook

如果可以将ajax调用更改为“GET”,请尝试以下更改:

Ajax调用:

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";

        }

     }

对我有用:)

Web API不是更适合这项任务的工具吗?您可以使用javascript将数据发布到WebAPI方法,默认情况下,它将以JSON的形式返回(使用内容类型)。xml等也一样。。。一个例子是的,的确如此。但我有一个用ASMX构建的小项目,它需要一种保持回调URL干净的方法。如果它是一个小项目,转换应该不难:正确地重定向。尝试使用GetListOfBook方法返回一个列表或其他内容,而不使用逻辑。如果重定向失败,则返回404error@Mate是的。但该方法从未被调用。如果我直接给它打电话,它会起作用。是的,它会起作用。我仅将IgnoreWebServiceCall添加到Global.asax。其他一切都保持不变。谢谢
    $(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>
    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);
    }
    [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";

        }

     }