views:

794

answers:

4

I use System.Web.Services.WebMethodAttribute to make a public static method of an ASP.NET page callable from a client-side script:

test.aspx.cs

[System.Web.Services.WebMethod]
public static string GetResult()
{
    return "result";
}

test.aspx

<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true" />

<script type="text/javascript">
    alert(PageMethods.GetResult());
</script>

The method works as it should, but if I load test.aspx with

Server.Transfer("test.aspx");

I receive "Unknown web method" error. After

Response.Redirect("test.aspx");

the page works well.

Could you tell me, please, what is a reason of the error and how can it also be avoided? Many thanks!

+1  A: 

Where do you receive the error - server or client?

If it's on the client, have a look at what it's trying to do. I suspect you'll find it's asking the original page to respond, rather than test.aspx.

Jon Skeet
I have the same issue mentioned here - http://www.west-wind.com/weblog/posts/152493.aspx
Alexander Prokofyev
+1  A: 

Server.Transfer transfers the processing of the page (at the server level) to the page you've specified, however the browser thinks you are still on the original page:

So, for example, you are on start.aspx and in the code behind you have Server.Transfer("test.aspx");

Your browser thinks you are still on start.aspx, and the javascript will be sending requests to page methods on start.aspx.

Using Response.Redirect your browser knows you are now on test.aspx and the requests are sent correctly.

Zhaph - Ben Duguid
A: 

It seems calling set_path solves the problem:

<script type="text/javascript">
    PageMethods.set_path("test.aspx");
    alert(PageMethods.GetResult());
</script>
Alexander Prokofyev
A: 

thank you Alexander Prokofyev