Failed to access webmethod on aspx.cs page

I am trying to call a web method with jquery ajax. However, the call returns a Not Found error. Attempting to access the method directly through the URL also returns a 404 error.

I have definitely added the option EnablePageMethods="true"to <asp:ToolkitScriptManager>on the main page.

Announcements.aspx

<script type="text/javascript">
    $(function () {
        $("#CreateBtn").click(function () {
            var announce = {};
            announce["title"] = "An Announcement";
            announce["body"] = "Announcement Body";

            $.ajax({
                type: "POST",
                url: "Announcements.aspx/AddAnnouncement",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(announce),
                success: function () {
                    alert("success!");
                },
                error: function (x, t, e) {
                    alert(t); //alerts "error"
                    alert(e); //alerts "Not Found"
                }
            });
            return false;
        })
    });
</script>

Announcements.aspx.cs

using System.Web.Services;

namespace MyProject.ContentTools
{   
public partial class Announcements : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]
    public static string AddAnnouncement(string title, string body)
    {
        var newTitle = title;
        var newBody = body;

        return "it worked!";
    }
}
}
+4
source share
4 answers

PageMethods ASP.NET MVC, , , aspx (, , URL- PageMethod, ). ( App_Start/RouteConfig.cs) :

routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });

PageMethod MVC.

+1

404, :

  • <asp:ToolkitScriptManager>. <asp:ScriptManager> ( ?);
  • jquery ajax, , <asp:ScriptManager> ;
0

. ScriptMethod ...

[WebMethod]    
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
        public static string AddAnnouncement(string title, string body)
        {
            var newTitle = title;
            var newBody = body;

            return "it worked!";
        }
and in your ajax method try to change data format.

    $.ajax({
                    type: "POST",
                    url: "Announcements.aspx/AddAnnouncement",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(title: announce.title, body: announce.body),
                    success: function () {
                        alert("success!");
                    },
                    error: function (x, t, e) {
                        alert(t); //alerts "error"
                        alert(e); //alerts "Not Found"
                    }
                });
0
source

Try it. You need to call PageMethods as below in your jquery ..

<script type="text/javascript">
    $(function () {
        $("#CreateBtn").click(function () {

            var title = "An Announcement";
            var body = "Announcement Body";
             PageMethods.AddAnnouncement(title,body,success,error);
        function success(result) {
                    alert("success!");
                }
        function error(result) {
                    alert(result); 
                }
            });
            return false;
        })
    });
</script>
-1
source

Source: https://habr.com/ru/post/1610617/


All Articles