如果您已实施ASP.NET路由,则ASP.NET中的PageMethods无法正常工作

前端之家收集整理的这篇文章主要介绍了如果您已实施ASP.NET路由,则ASP.NET中的PageMethods无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个网络方法
  1. [System.Web.Services.WebMethod]
  2. public static string getCharacterstics(int product_id,int lang_id)
  3. {
  4. // code goes here...
  5. }

我想使用PageMethods访问它,例如:
(假设我在ScriptManager中启用PageMethods的条件):

  1. <script type="text/javascript">
  2. $(document).ready(
  3. function () {
  4. $('#characterstics').html("loading");
  5. // '<%= product_id_property %>','<%= this.LanguageID %>'
  6. PageMethods.getCharacterstics(0,OnSave);
  7. }
  8. );
  9.  
  10. function OnSave(result) {
  11. }
  12. </script>

我得到错误“http动词帖子访问路径..是不允许的”

搜索了它并搜索了SO,但没有得到任何有关它的解决方案基于ASP.NET路由.

我认为,由于asp.net路由,服务方法无法访问.

另外我认为我甚至不能使用JSON,因为asp.net路由也是如此.

任何帮助表示赞赏.

更新:

如果我用这个网址运行页面

  1. http://localhost:2606/searchdetail.aspx

Web方法已成功执行.

现在

我有这样的路由:

  1. routes.MapPageRoute("searchdetail","searchdetail/{ID}","~/searchdetail.aspx");
  2. routes.MapPageRoute("searchdetail","searchdetail","~/searchdetail.aspx");

set_path()仅适用于案例2,即没有ID,但不适用于案例1

如果我试试

  1. http://localhost:2606/searchdetail

它工作正常

但如果我尝试使用:

  1. http://localhost:2606/searchdetail/123

它给出了对象所期望的错误.

所以set_path()是我应该写的选项.

解决方法

目前,WebMethods不能与Routing框架透明地工作.有一个工作.您必须通过在javascript中执行以下操作直接访问PageMethods:
  1. PageMethods.set_path('/the/path/to/your/page.aspx');
  2. PageMethods.YourMethod(params,onSuccess,onFailure);

我希望这有帮助.

猜你在找的asp.Net相关文章