Missing PageMethods on an MS AJAX Page?
You ever have one of those days when just nothing wants to go right? A couple of days ago I was working on my session code for ASP.NET Connections. One of my sessions is on Dealing with Long Running Requests and one of the examples uses AJAX to offload the processing to messaging engine and optionally a separate process/machine. I've had this demo around for some time and originally I built it using Microsoft AJAX Beta 2 or one of the release candidates.
So I dusted off the old demo and sat down to update it for the RTM version which should have been pretty quick. The first part was getting a Web.config file that actually has all the MS AJAX settings in them. The original Web.config had the beta settings so I thought the easiest way to do this was to create another project that is all set up for MS AJAX and then copy that web.config into the current project (after renaming the old one) and then simply migrating my actual configuration settings ( ConnectionStrings, AppSettings, and one handler) over to the new web.config.
I also had to tweak a number of replaces for $() syntax to $get() and a couple of changes in one of the client side controls used in the sample's JavaScript code. That's where I thought it would end.
Well it didn't. The sample uses JSON callbacks using PageMethods, which saw some changes late in the MS AJAX cycle. PageMethods were made static so they no longer ran in the context of the page at all (which kind of defeats the whole purpose of PageMethods in the first place - the old feature set allowed for access to the controls of the page and full POST data). Again so had to make some adjustments to the code to change to static methods and make sure that each of the methods actually was stateless and didn't use any of the form's objects (like the MessageManager). Again no big deal.
But at that point the page still would not work. Whenever I'd run the page, I'd get a JavaScript errors saying that PageMethods is not defined. Quickly checking the network trace with FireBug confirmed that the script resource that normally contains the AJAX JSON proxy never got created.
I checked:
- Methods marked as [WebMethod]
- Web Methods are static
- ScriptManager on page
- EnablePageMethods set on the script manager
No luck. I checked, re-checked my settings but no matter what I tried it just... would... not... work. I even went so far as to create a new page dropped a script manager on it, created a couple of WebMethods and called them from client script to make sure I wasn't doing something wrong and sure enough that worked just fine. It's just in the particular page that it wouldn't work.
Eventually - after about an hour of screwing around with this - I just created a Web Service and called that instead and that worked immediately. I honestly don't know what was wrong with that page if anything. I even called a friend of mine to check out the page with GotoMeeting (in case I couldn't see the forest for the trees 'ya know - wouldn't be the first time) but he didn't see anything either.
So... anybody ever seen this before? PageMethods not working on a page failing to send the proxy code to the client?
In the end I suppose that's a good thing anyway. PageMethods became pretty much useless anyway when they became static in RTM. There's really no benefit for using them (other than keeping your code encapsulated in the Page which is questionable to begin with) and it actually made the sample a bit cleaner. The message service checking and processing code now all runs in the service class which is easier to demonstrate separately anyway - once you find it buried in the APP_CODE directory...
Other Posts you might also like
- Adding minimal OWIN Identity Authentication to an Existing ASP.NET MVC Application
- Resolving Paths To Server Relative Paths in .NET Code
- Map Physical Paths with an HttpContext.MapPath() Extension Method in ASP.NET
- Back to Basics: Rewriting a URL in ASP.NET Core
- Getting the Client IP Address in ASP.NET Core
The Voices of Reason
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
As someone already mentioned, the ScriptManager EnablePageMethods attribute.
Something else I have run into is that you now also have to mark the method with the ScriptMethod attribute.
Jay Kimble
PS. if you wanna send me the code I can probably get it working for ya...
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
I also had problems with PageMethods, not noticing any changes in the code in the PageMethod method. So every time i made a change, i had to clean the solution and rebuild, just as the other guy said earlier.
# re: Missing PageMethods on an MS AJAX Page?
One note about PageMethod goodness. Generally, if some functionality will be accessed by multiple pages in the site, then you would use the web service route; otherwise using a PageMethod is preferable. For one, it reduces your application’s security footprint and provides a level modularity to your code.
Hope this help.
J. Johnson
# re: Missing PageMethods on an MS AJAX Page?
Just to clarify I did have EnablePageMethods=True on the ScriptManager. And I don't think caching was the issue because I saw the behavior both in IE and FireFox plus I reloaded IIS several times.
But it's interesting to hear that others have seen this behavior and so I don't feel like a complete dolt <s>...
# re: Missing PageMethods on an MS AJAX Page?
Grewal
# re: Missing PageMethods on an MS AJAX Page?
I also had the same problem with PageMethods, try to add service reference to <asp:ScriptManager>.
<Services> <asp:ServiceReference Path="~/EcmService.asmx" /> </Services>
Also in service create at least one method with [WebMethod]
attribute, then try to use PageMethod. In my case it helps.
# re: Missing PageMethods on an MS AJAX Page?
I saw "PageMethods is undefined" error when my WebMethod was inside user control (even declared as static). Similar problem was inside master page. But in aspx it worked.
In some cases another attribute may help:
[System.Web.Script.Services.ScriptMethod]
Igor
# re: Missing PageMethods on an MS AJAX Page?
But it's definitely disconcerting to see that there are a issues with PageMethods not working in some scenarios. It doesn't sound like all the reports here are operator error like I was suspecting of myself <g>..
# re: Missing PageMethods on an MS AJAX Page?
My Page Method works perfectly in development environment, however I cannot for the life of me get in to work in a hosting environment.
Mark
# re: Missing PageMethods on an MS AJAX Page?
<script language="C#" runat="server">
[WebMethod]
public static int Add(int num1, int num2)
{
return num1 + num2;
}
</script>
Whats weird is if you put the function in the code-behind, the proxy will be generated sometimes, and other times not.
- Mark
# re: Missing PageMethods on an MS AJAX Page?
anyway, as already was said due to the static-only character of the pagemethods i need to get rid of using pagemethods... some things were better in "atlas".
# re: Missing PageMethods on an MS AJAX Page?
I have to go through a lot of pain to get pagemethods working.
Here are things to check
1. Static Method should be in actual page (content). It will not work if method is in User Control or Master page.
2. Set ContentManager property "EnablePageMethods" to true.
# re: Missing PageMethods on an MS AJAX Page?
See
http://xsation.blogspot.com/2007/11/forget-page-methods-move-on-to-script.html
For the hello world of the script service.
Mark
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
I was able to get it to work by using something like the following - it wasn't working for me until I used the form tag:
<form runat="server">
<asp:scriptmanager runat="server" EnablePageMethods="true" />
<script language="C#" runat="server">
[WebMethod]
public static void test( )
{
}
</script>
</form>
# re: Missing PageMethods on an MS AJAX Page?
Also had similar problems but only with MasterPage or user controls. As was suggested before putting the method on the main .aspx page seems to work. If you want to be able to call on all pages then put the method in a base page class. Should look something like:
public class MyBasePageClass : System.Web.UI.Page { [System.Web.Services.WebMethod(true)] // true if you're using Session [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] // httpget isn't necessary but often a good idea public static string MyCommonWebMethod() { return "howdy"; } }
Of course you'll also need to EnablePageMethods in the ScriptManager.
Does seem a bit too difficult. But it is kinda handy when it works.
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
Is your webservice in page or is it in a master page or control? The way the event hooks up to the Script Manager, it must be in a page. I just discovered this same issue.
Jason
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
It was solved by a clean rebuild of the entire project.
Some kind of VS caching issue?
# re: Missing PageMethods on an MS AJAX Page?
Oh this stuff gets frustrating sometimes.
I had a default.aspx page that did a Server.Transfer to another page that I wanted to call a PageMethod from. Kept getting the PageMethod is undefined error or it couldn't find the method. Checked Event Log and saw "Unknown web method" errors, huh?
After quite some time I moved the method to the Default page, not the page where it's called and it sorta worked for a while.
Then I realized that maybe the Server.Transfer was contributing to the problem and changed that to a Response.Redirect and voila - it works reliably now! With the method on the ASPX page or in code-behind.
Fortunately, the sample here
http://www.asp.net/AJAX/Documentation/Live/tutorials/ExposingWebServicesToAJAXTutorial.aspx
does work and helped me keep the faith that it really does work and the problem was in my code.
# re: Missing PageMethods on an MS AJAX Page?
switching the Server.Transfer from my first screen to the 2nd one was the only thing that seemed to work for me. Guess you can't use the server transfer when the target page uses page methods...
Thanks!
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
I just encountered this problem as well, it's related to the autogenerated proxy scripts being cached as far as I can understand. What I wonder is: On my production server, I have content expiration enabled and .js are cached at client, will this be a problem once I've deployed a new version of a page containing a changed Page method. Will clients not download the new clientscript files due to the content expiration or will ASP.NET regenerate the scripts. Have you got any clue?
Thanks for a great blog!
/Mats
# re: Missing PageMethods on an MS AJAX Page?
THere's some bug in the codepath it looks like based on the decriptions above that is causing the script reference inclusion to be omitted by ScriptManager it seems.
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
"Hi,
No, you can't define the PageMethod inside a control. It must be defined in a page.
When the EnabledPageMethod property of the ScriptManager instance on the page is set to true, it'll inject script proxy for each web method defined in the page.
And the script proxy can't be generated if it's defined in a control. "
Sounds like a major design flaw in .net Ajax extensions.
Why is it that everytime I find an enhancement I think may be cool and easy to use in .Net it always ends up being way more complicated than I ever imagined?
# re: Missing PageMethods on an MS AJAX Page?
If I am running on my Dev box, I just stop the running instance. Also, when I deploy, I try to make sure to restart the site.
# re: Missing PageMethods on an MS AJAX Page?
One simple reason that might be forgotten can be if inline scripts are used in the aspx page the script tag should be defined as runat="server"
Methods also required with WebMethod attribute
So the aspx page itself must also import the Namespace="System.Web.Services" on the page declaration.
I have read somewhere that the server side scripts should not be written between HEAD html tags. But I'm not sure about it.
Thanks for your work on this web site. You share alot for developers.
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
The proxies seem to be getting generated (although maybe they are old ones that are not getting rebuilt.)
I am on VS2008, 3.5 SP1
# re: Missing PageMethods on an MS AJAX Page?
From a customer point of view, it's a lot less professional a URL to see, and a lot longer, and a lot more error prone.
From a search engine point of view, a redirect is considered not a "real url" by things like Google, whereas a Server.Transfer is invisible to them. I've been using that for some time to make sure that google indexed all my dynamically generated product pages properly.
I'd rather ditch my new ajax widget than ditch server.Transfer, so I'm really hoping there is a better solution.
Anyone?
# re: Missing PageMethods on an MS AJAX Page?
I am also facing the same problem, but in one of my test web application which I created earlier its work fine and even not a single time it give a error like "PageMethods undefined".
In both cases I m using the same scenario, a default page, a web user control and a helping js file. but one is going fine and other is going off ;(
# re: Missing PageMethods on an MS AJAX Page?
Cheers, Stu
# re: Missing PageMethods on an MS AJAX Page?
close the Visual Studio
Clear de temporary .net
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
Open the project/website again
and run.
# re: Missing PageMethods on an MS AJAX Page?
[WebMethod]
public static string ReallyCoolUserControlWebMethod(string x, string y)
{
return ASP.your_user_control.ReallyCoolUserControlWebMethod(x, y);
}
(ASP.your_user_control is a registered usercontrol on page)
# re: Missing PageMethods on an MS AJAX Page?
Clearing the cache (in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files) worked for me! Thank you to the posters who suggested this.
# re: Missing PageMethods on an MS AJAX Page?
To fix you must reset the path to the page method right before you call your method.
e.g.,
PageMethods.set_path('pagename.aspx') // without the leading '/' which is in the generated proxy
PageMethods.MyMethod(params[], OnSuccess, OnFail);
# re: Missing PageMethods on an MS AJAX Page?
This error will present itself as a 401 error:
Sys.Net.WebServiceFailedException: The server method 'MyMethod' failed with the following error: System.InvalidOperationException--Authentication failed.
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
if you are using cookieless sessions - the generated script proxy will set the page path to '/pagename.aspx' which will break the PageMethod.
To fix you must reset the path to the page method right before you call your method.
e.g.,
PageMethods.set_path('pagename.aspx') // without the leading '/' which is in the generated proxy
# re: Missing PageMethods on an MS AJAX Page?
I don't know what I move, write or delete in my program, but when I restart over from zero the code, it works with the same stuff that I had work. Sadly, I was 3 hours late at work to find this...
And as u said, just have to:
• Methods marked as [WebMethod]
• Web Methods are static (Shared in VB)
• ScriptManager on page
• EnablePageMethods set to "True" the script manager
This is an helpful example:
http://geeks.ms/blogs/mrubino/archive/2008/12/18/serializar-deserializar-json-en-asp-net.aspx?CommentPosted=true
Regards from México!!!
# re: Missing PageMethods on an MS AJAX Page?
The fix was to do a Clean Solution then a Rebuild Solution. Thanks for the suggestions for this...thought I was loosing my mind.
Also, I found no difference whether the code for the web method was in the .aspx file or the code behind. Same issue for me...VS is caching the page method and won't see any changes to it until I clean and rebuild. Crappy way to develop but I'll have to live with it I guess.
# re: Missing PageMethods on an MS AJAX Page?
PageMethods.set_path('pagename.aspx')
works well for the Master Page case as well.
In case of MasterPage the service path sets to the default page of the site.
You should set it to the page where your server side page methods are.
# re: Missing PageMethods on an MS AJAX Page?
anybody knows why?
thanks!
# re: Missing PageMethods on an MS AJAX Page?
I agree with Ameet, two posts above. I'm using a Master Page and setting the service path FINALLY fixed my problem.
Thank you!
# re: Missing PageMethods on an MS AJAX Page?
I had:
...
$("#testAuto").autocomplete(window.location.pathname + "/NewProcedure.aspx/MyPageMethod", {
...
and changed it to:
...
$("#testAuto").autocomplete( "NewProcedure.aspx/MyPageMethod", {
...
did not recieve the unknown issue after that.
# re: Missing PageMethods on an MS AJAX Page?
I'm having the same problem, its really fustrating because you can't tell if the problem is related to something you've just done, or something you did before it cached. Is there anyway to ensure that caching doesn't become an issue. Highly annoying!!
If developers are using page methods heavily in their projects how the heck do they go about debugging their code if they constantly run into this awful mess?!?
# re: Missing PageMethods on an MS AJAX Page?
was keeping on occuring what ever.
Here's the solution.
Uncomment these lines in web.config.
<jsonSerialization maxJsonLength="500">
<converters>
<add name="DataSetConverter" type=&
quot;Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter,
Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&
quot;/>
<add name="DataRowConverter" type=&
quot;Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter,
Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&
quot;/>
<add name="DataTableConverter" type=&
quot;Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter,
Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&
quot;/>
</converters>
</jsonSerialization>
Read More
<A href="http://muruganad.com/ASP.NET/ajax_PageMethods_System_InvalidOperationException.html">http://muruganad.com/ASP.NET/ajax_PageMethods_System_InvalidOperationException.html</a>
Thanks!
<a href="http://muruganad.com">Murugan Andezuthu Dharmaratnam</a>
# re: Missing PageMethods on an MS AJAX Page?
Adding "using System.Web.Services;" to my masterpage worked for me, even though my scriptmanager had EnablePageMethods="true" on the same masterpage.
# re: Missing PageMethods on an MS AJAX Page?
So I call my page methods like this.
PageMethods._staticInstance.[FunctionName]
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
This is very strange problem and its been more than 1 and half day, but I am still unable to solve it, I tried everything which is written above in the comments suggested be different people.
In my case the wiered part is, my code works fine in IE and PageMethods call take place properly and do the work as expecte, but when EXACTLY same code I try to run through Firefox, it always call OnFailed method of PageMethods call.
In same web app I have more webmethods and pagemethods calls on different pages and they all run fine on IE and Firefox, only this specific page is giving this problem.
I am still spending time on it to get rid off this problem. :(
Regards
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
C#...
[WebMethod]
public static void DoSomething(bool delete)
{
...
}
javascript:
PageMethods.DoSomething(true);
.NET creates a javascript proxy for the webmethod. As you can see, my webmethod parameter name was "delete". This isn't reserved in C#, but is in Javascript. Renaming this parameter solved the problem.
# re: Missing PageMethods on an MS AJAX Page?
http://www.codeproject.com/KB/aspnet/CallCodeBehindByJS.aspx
This worked great first time and I'm not looking back! The server side code doesn't have to be static which is also a plus. No more PageMethods.
# re: Missing PageMethods on an MS AJAX Page?
When they're working they work great. But randomly when trying to set them up they'll just refuse to work. Rarely do I see the PageMethods javascript get produced correctly first time
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
# re: Missing PageMethods on an MS AJAX Page?
I realize this is an old blogpost but for anyone else having this problem: try putting the code-behind inline in the actual aspx page - this works for me.
<script language="C#" runat="server"> [System.Web.Services.WebMethod] public static void GetDetails(string Id) { // Do stuff here. } </script> <script type="text/javascript"> function GetDetails(Id) { PageMethods.GetDetails(id); } GetDetails(42); </script>
# re: Missing PageMethods on an MS AJAX Page?
<system.web>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx"
type="Microsoft.Web.Script.Services.ScriptHandlerFactory"
validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>