Skip to main content
 首页 » 编程设计

c#之从jQuery C#调用webmethod在.net aspx页面中返回404错误

2024年01月18日26over140

在这里一定缺少一些基本的东西。我在本地运行的aspx页面中从jQuery调用webmethod时遇到404错误。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="empJquery.aspx.cs" Inherits="webApiOracle.empJquery" %> 
 
<!DOCTYPE html> 
 
<html lang="en"> 
<head runat="server"> 
    <title>jquery emps</title> 
    <script src="Scripts/jquery-1.10.2.min.js"></script> 
    <link href="../Content/bootstrap.min.css" rel="stylesheet" /> 
    <script type="text/javascript"> 
 
 
        $(document).ready(function () { 
            getEmployees(); 
 
        }); 
 
        function getEmployees() { 
            jQuery.ajax({ 
                url: 'empJquery.aspx/Test', 
                type: "POST", 
                contentType: "application/json; charset=utf-8", 
                dataType: "json", 
                beforeSend: function () { 
                    alert("Start!!! "); 
                }, 
                success: function (data) { 
                    alert("a"); 
                }, 
                error: function (msg) { alert("Sorry!!! "); } 
 
            }); 
        } 
 
    </script> 


然后在页面后面的代码中

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using webApiOracle.Models; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
 
 
namespace webApiOracle 
{ 
    public partial class empJquery : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
 
        } 
 
        [System.Web.Services.WebMethod] 
        public static string Test() 
        { 
 
            return "Success"; 
        } 
 
    } 
 
} 


每当我运行时,我都遇到404错误,尝试将URL更改为/empJquery.aspx/Test,我也尝试了localhost / empJQuery.aspx / Test和localhost:48784 / empJquery.aspx / Test,但我似乎无法获取它。我是否需要在Web配置中添加一些内容?我正在运行.net 4.5
谢谢

请您参考如下方法:

好的,我发现它的应用程序是混合的MVC,Web API,ASPX解决方案,并且我的路由配置让我很困惑,所以我必须添加:

 routes.IgnoreRoute("{page}.aspx/{*webmethod}");