0

I'm trying to have an index.aspx file under the App_Code directory, which will pick up on api calls performed by ajax.

Example:

File structure in project

(This simply has an App_Code directory with 1 index.aspx file under it. Finally, there is a index.html file outside of this directory for the home page.)

index.aspx code front

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication4.index" runat="server" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index page</title>
</head>
<body>
    <form id="form1" runat="server">

    </form>
</body>
</html>

index.aspx code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication4
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String strAPI = Request.Form["api"];

            switch(strAPI)
            {
                case "test":
                    Response.Write("It worked!");
                    break;
            }

            //lstTest.Items.Add("test");
        }
    }
}

index.html code

<!DOCTYPE html>

<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        window.onload = function () {
            $.ajax({
                url: 'App_Code/',
                data: 'api=test',
                dataType: 'text',
                type: 'POST',
                contentType: 'application/x-www-form-urlencoded',
                success: function (data)
                {
                    debugger;
                },
                error: function (data, status)
                {
                    debugger;
                }
            });
        };
    </script>
</head>

<body>
    <h2>TEST</h2>
</body>

</html>

I've gotten this to work before (but cannot recall how). The error that I'm receiving is 'IIS 10.0 Detailed Error - 404.8 - Not Found' in the ajax call's error event. Basically it's never getting into the page load method of the index.aspx file.

1
  • 2
    Why would you do that? App_Code it's not for pages
    – hardkoded
    Dec 31, 2017 at 21:39

2 Answers 2

0

From your screenshot, it seems that you project type is web application, not website.

Firstly, do not use the App_Code folder for web application projects. It was meant to be used in website projects to put the code files that are not attached to a page (i.e. not code-behind). In web application projects, you can put the code files anywhere you want, so the App_Code folder is not required and offers no special functionality. You can still use it like any other folder, but it is recommended to use a better named folder instead.

So you should avoid putting aspx pages in the App_Code folder, because it is confusing and makes no sense in web application projects. Use a better named folder for that. For that reason, I will refrain from posting a solution to make it work, because you shouldn't be doing it in the first place.

Instead, rename the App_Code folder to something meaningful, say API and then use that name in your Ajax like /api.

2
  • Thanks, it was my understanding that App_Code protects files, but maybe it's more for classes such as ones dealing with a database connection.
    – User10505
    Jan 1, 2018 at 15:03
  • Protecting files means making them not accessible. Example, protecting dll files and the web.config file. You don't want your visitors to type yourdomain.com/web.config and then be able to see all your configurations and database connection string. But your aspx pages, you want them accessible, otherwise what's the point of having a web application if the pages are not accessible? If you want your aspx pages to only be accessible to members, then you need to look for authentication. Completely different concept. Jan 1, 2018 at 16:20
0

Hello in ajax call try to change the url like : url: 'App_Code/Index.aspx/Page_Load' I am not sure if it is the answer but try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.