1
votes

My ajax call works perfectly fine if my aspx page is directly below my web project.( eg., mysite.web/LeadSummary.aspx ) But if my aspx page is inside another folder (eg., mysite.web/Summary/LeadSummary.aspx) ,my ajax call fails and is always showing 404 error.

I tried as per similar post here .But this did not worked for me . Any suggestions to make this working.

Here is my LeadSummary.aspx page ( is inside a subfolder within my webproject, ie., 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>

Here is the code behind for the above aspx page

    [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();
}

And here is the error message I found in DOM response text

"

<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]: The controller for path '/summary/LeadsSummary.aspx/BindEmployees' was not found or does not implement IController.

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

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

at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& 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& completedSynchronously)

-->" status

404

Any idea why my Ajax call is showing this error ? Any suggestions to resolve this error?

Update

I added the below lines to my RouteConfig and it worked

routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
1
If you have moved it to a folder, then why don't you add it in your ajax url - <%=ResolveClientUrl("Summary/LeadsSummary.aspx")%>/BindEmployees ? - Guruprasad J Rao
I have already tried that ,but no luck - roney
if u using hybrid app,u can try this to add route routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); - sakir
I tried adding routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); but still getting same erros - roney
I also tried adding routes.IgnoreRoute("{page}.aspx/{*webmethod}"); but still getting same erros - roney

1 Answers

2
votes

I added

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

to my route configuration and it is working fine now