无法访问aspx.cs页面上的webmethod

本文关键字:webmethod cs 访问 aspx | 更新日期: 2023-09-27 18:16:42

我正在尝试使用jquery ajax调用webmethod。但是,调用返回Not Found错误。试图通过URL直接访问该方法也会返回404错误。

我确保将EnablePageMethods="true"参数添加到母版页的<asp:ToolkitScriptManager>

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!";
    }
}
}

无法访问aspx.cs页面上的webmethod

如果您在ASP. js中使用PageMethods。. NET MVC项目,您可能需要忽略aspx页面的路由(因此,基于它们的PageMethod url)。在您的路由注册(通常在App_Start/RouteConfig.cs)中,添加以下行:

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

这应该允许PageMethod请求通过而不受MVC路由的干扰。

我不确定404问题究竟是什么,但还有一些其他的事情:

  • 你正在使用<asp:ToolkitScriptManager>。这应该是<asp:ScriptManager>(有区别吗??);
  • 如果使用jquery的ajax,我认为<asp:ScriptManager>甚至不需要;

试试这个,你必须在你的方法中添加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"
                    }
                });

试试这个,你必须在jquery中像下面这样调用PageMethods .

<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>