PageMethods in ASP.NET failed to work if you have implemented ASP.NET routing

I have a web method:

[System.Web.Services.WebMethod] public static string getCharacterstics(int product_id, int lang_id) { // code goes here... } 

I want to access it using PageMethods, for example: (Provided that I have Enable PageMethods in ScriptManager):

 <script type="text/javascript"> $(document).ready( function () { $('#characterstics').html("loading"); // '<%= product_id_property %>' , '<%= this.LanguageID %>' PageMethods.getCharacterstics(0 ,0 , OnSave); } ); function OnSave(result) { } </script> 

I get the error message "http-verb message to access the path .. not allowed"

I searched for it and searched for SO too, but got no solution regarding it. Based on ASP.NET Routing.

I believe that due to asp.net routing, service methods are not available.

Also, I think I can't even use JSON due to asp.net routing.

Any help is appreciated.

Updated:

If I run a page with this url:

 http://localhost:2606/searchdetail.aspx 

The web method completed successfully.

Now

I have a routing like this:

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

The set_path () function will work only for case 2 without an identifier, but does not work with case 1

if i try

  http://localhost:2606/searchdetail 

It works great

but if I try to use:

 http://localhost:2606/searchdetail/123 

It gives the error expected by the object.

So set_path () is the option I have to write.

+4
source share
2 answers

WebMethods currently does not work transparently with the routing infrastructure. Works. You should access PageMethods directly by doing the following in your javascript:

 PageMethods.set_path('/the/path/to/your/page.aspx'); PageMethods.YourMethod(params, onSuccess, onFailure); 

Hope this helps.

+11
source

I have come across this all the time. If routing is enabled, if I add a value to the path, it will fail again. This solution is a bit hacky, but it seems to work consistently.

Create a server control hyperlink where the navigation URL refers to itself, then as soon as the control displays, take href and use it for set_path

This is due to a set_path issue not related to the correct location if you called

 <asp:HyperLink ID="hlPage" runat="server" NavigateUrl="~/user.aspx" ClientIDMode="Static"></asp:HyperLink> <script> $(document).ready(function () {PageMethods.set_path($('#hlPage').attr('href'));}) </script> 
+2
source

Source: https://habr.com/ru/post/1381425/


All Articles