Ajax jquery在aspx代码后面调用WebMethod内部子文件夹404错误

时间:2015-10-26 03:29:19

标签: jquery asp.net ajax asp.net-mvc c#-4.0

如果我的aspx页面直接位于我的web项目下面,那么我的ajax调用完全正常。(例如,mysite.web / LeadSummary.aspx)但是如果我的aspx页面在另一个文件夹中(例如,mysite.web / Summary) /LeadSummary.aspx),我的ajax调用失败,并且始终显示404错误。

我按照类似的帖子here进行了尝试。但这对我没用。 任何使这项工作有效的建议。

这是我的LeadSummary.aspx页面(位于我的webproject中的子文件夹中,即,mysite.Web / summary / LeadsSummary.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LeadsSummary.aspx.cs" Inherits="Summary_LeadsSummary" %>

  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">
    <title>Export Grid to Excel</title>
   
      <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
      
      <script type="text/javascript">
        $(document).ready(function() {
          $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '/summary/LeadsSummary.aspx/BindEmployees',
            data: "{}",
            dataType: "json",
            success: function(result) {
              for (var i = 0; i < result.d.length; i++) {
                $("#gvData").append("<tr><td>" + result.d[i].FirstName + "</td><td>" + result.d[i].LastName + "</td><td>" + result.d[i].City + "</td><td>" + result.d[i].Country + "</td></tr>");
              }
            },
            error: function(result) {
              debugger;
              alert("Error");
            }
          });


        }); //doc ready
      </script>
  </head>

  <body>
  
    <br />
    <form id="form1" runat="server">
      Search :
      <asp:TextBox ID="txtSearch" runat="server" Font-Size="20px" onkeyup="Search_Gridview(this, 'gvData')"></asp:TextBox>
      <br />
      <br />
      <hr />
      <asp:GridView ID="gvData" runat="server" CellPadding="4" ShowHeaderWhenEmpty="true" ForeColor="#333333">
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
      </asp:GridView>
    </form>
  </body>

  </html>

以下是上述aspx页面背后的代码

    [WebMethod]
public static Employee[] BindEmployees()
{


    var employeeList = new Employee[]
    {
        new Employee { FirstName="Harry", LastName="Fields" , City="Bellevue",Country="USA" },
        new Employee { FirstName="Alex", LastName="Yert" , City="Westj",Country="Japan" },
        new Employee{ FirstName="Mool", LastName="Yerwfd" , City="WWSw",Country="Canada" }
   };

    return employeeList.ToArray();
}

这是我在DOM响应文本中找到的错误消息

  

<head>

    <title>The resource cannot be found.</title>

    <meta name="viewport" content="width=device-width" />

    <style>

     body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

     p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}

     b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}

     H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

     H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

     pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}

     .marker {font-weight: bold; color: black;text-decoration: none;}

     .version {color: gray;}

     .error {margin-bottom: 10px;}

     .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

     @media screen and (max-width: 639px) {

      pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }

     }

     @media screen and (max-width: 479px) {

      pre { width: 280px; }

     }

    </style>

</head>



<body bgcolor="white">



        <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>



        <h2> <i>The resource cannot be found.</i> </h2></span>



        <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">



        <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.

        <br><br>



        <b> Requested URL: </b>/summary/LeadsSummary.aspx/BindEmployees<br><br>



        <hr width=100% size=1 color=silver>



        <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.79.0



        </font>



</body>

[HttpException]:找不到路径'/summary/LeadsSummary.aspx/BindEmployees'的控制器,或者没有实现IController。

在System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)

在System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)

在System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&amp; controller,IControllerFactory&amp; factory)

at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback callback,Object state)

at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback callback,Object state)

at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData)

at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)

- &gt;”中 状态

404

知道为什么我的Ajax调用显示此错误?有任何解决此错误的建议吗?

更新

我在RouteConfig中添加了以下行,并且它有效

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

1 个答案:

答案 0 :(得分:1)

我添加了

  

routes.IgnoreRoute(“{ allaspx}”,new {allaspx = @“。 .aspx(/.*)?”});

到我的路线配置,现在工作正常