【问题标题】:Ajax jquery call to WebMethod in aspx code behind inside subfolder 404 errorAjax jquery 在子文件夹 404 错误后面的 aspx 代码中调用 WebMethod
【发布时间】:2016-01-25 02:19:57
【问题描述】:

如果我的 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& 控制器, IControllerFactory& 工厂)

在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态)

在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调, 对象状态)

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

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

在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

-->" 状态

404

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

更新

我在我的 RouteConfig 中添加了以下几行,它起作用了

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

【问题讨论】:

  • 如果你已经将它移动到一个文件夹中,那你为什么不把它添加到你的ajax url - &lt;%=ResolveClientUrl("Summary/LeadsSummary.aspx")%&gt;/BindEmployees 中呢?
  • 我已经尝试过了,但没有运气
  • 如果你使用混合应用,你可以试试这个添加路由路由。IgnoreRoute("{resource}.aspx/{*pathInfo}");
  • 我尝试添加 routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");但仍然遇到同样的错误
  • 我也尝试添加 routes.IgnoreRoute("{page}.aspx/{*webmethod}");但仍然遇到同样的错误

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


【解决方案1】:

我加了

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多