ASP.NET PageMethods Break When Using ASP.NET Routing

11/14/2009 10:13:56 AM

I was doing some work yesterday to add comments to this site and came across an issue I couldn't resolve.  I had created a public webmethod in my codebehind and wanted to expose it as a PageMethod so I could perform some actions with AJAX.  I set up all of the necessities:

  • Scriptmanager has EnablePageMethods set to True - Check!
  • Reference to System.Web.Services - Check!
  • Method is Public and Shared (static) - Check!
  • Method is decorated with <WebMethod(True)> - Check!

When I went to call the method from my page, it kept returning an error: "The service method 'methodname' failed"

I started googling and everything I found was just suggestions to enable page methods.  I decided to see what happened if I fired the PageMethod call from the console in FireBug.  When I opened the console I saw there was an error saying that the page "url_path_to_page_with_routing_alias/pagemethod_name" was not found.  At this point I knew that ASP.NET Routing was the culprit and modified my google searches accordingly.  I came across one post in the ASP.NET Forums that stated a similar problem.

The research done by the poster of the thread seemed to indicate that the issue was with how Routing works and hopefully would be fixed at in a later version.  They did manage to offer a work around that has fixed my issue. Simply add the following before you need to make a PageMethod call from a routed page and you can set the correct path to the method.

 // set correct path to page 
PageMethods.set_path('/yourpage.aspx'); 
// call PageMethod as you normally would 
PageMethods.YourMethodName(param, onSuccess, onFailure); 

That took me longer to figure out than I would like to admit.  I just couldn't understand what I was doing wrong when I had used PageMethods before with no issues.  It just goes to show you that adding more and more "cool" things to the pile can sometimes make waves.  Hopefully this saves someone else a bit of headache too.

Tags:ASP.NET
a393767f-1f0b-4438-9b28-d6ab8ded4cd3

Comments:

No comments yet
Name:
Email: (Your email will not be displayed or distributed in any way)
Comment:
  • © brentman.com 2009-2012 | This site licensed under a Creative Commons license
  • The opinions expressed on this site are my own and not those of my employer.
Log In
 
 
login