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

时间:2015-10-07 16:10:07

标签: c# jquery asp.net webforms webmethod

我试图用jquery ajax调用web方法。但是,该调用返回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!";
    }
}
}

4 个答案:

答案 0 :(得分:1)

如果您在ASP.NET MVC 项目中使用PageMethods,则可能需要忽略aspx页面的路由(因此,需要忽略基于它们的PageMethod URL)。在路线注册中(通常在routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" }); ),添加以下行:

{{1}}

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

答案 1 :(得分:0)

我不确定404问题,但还有其他一些事情:

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

答案 2 :(得分: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"
                    }
                });

答案 3 :(得分:-1)

试试这个..你必须在你的jquery中调用如下的PageMethod ..

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