Error "Unknown Web Method" after Server.Transfer
I am using System.Web.Services.WebMethodAttribute to make a public static method of an ASP.NET page called from the 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 am getting an "Unknown Web Method" error. After
Response.Redirect("test.aspx");
the page works well.
Could you please tell me what is causing the error and how you can avoid it? Many thanks!
source to share
Server.Transfer shifts the page processing (at the server level) to the page you specified, however the browser thinks you are still on the original page:
So, for example, you are at start.aspx and in the code behind you have Server.Transfer ("test.aspx");
Your browser thinks you are still at start.aspx and javascript will send requests for page methods at start.aspx.
With Response.Redirect, your browser knows you are currently on test.aspx and requests are being sent correctly.
source to share