Reputation: 34515
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!
Upvotes: 2
Views: 2303
Reputation: 34515
It seems calling set_path solves the problem:
<script type="text/javascript">
PageMethods.set_path("test.aspx");
alert(PageMethods.GetResult());
</script>
Upvotes: 1
Reputation: 26956
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.
Upvotes: 1
Reputation: 1500775
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.
Upvotes: 1