从ajax调用访问index.aspx文件(在App_Code中)?

问题描述 投票:0回答:2

我正在尝试在App_Code目录下有一个index.aspx文件,该文件将接收ajax执行的api调用。

例:

File structure in project

(这只是一个App_Code目录,下面有一个index.aspx文件。最后,主页的这个目录之外有一个index.html文件。)

index.aspx代码前面

<%@ 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代码背后:

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代码

<!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>

我之前已经开始工作(但不记得是怎么回事)。我收到的错误是ajax调用错误事件中的“IIS 10.0详细错误 - 404.8 - 未找到”。基本上它永远不会进入index.aspx文件的页面加载方法。

c# asp.net ajax
2个回答
0
投票

从截图中看,您的项目类型似乎是Web应用程序,而不是网站。

首先,不要将App_Code文件夹用于Web应用程序项目。它旨在用于网站项目,以放置未附加到页面的代码文件(即不是代码隐藏)。在Web应用程序项目中,您可以将代码文件放在任何需要的位置,因此不需要App_Code文件夹,并且不提供任何特殊功能。您仍然可以像任何其他文件夹一样使用它,但建议使用更好的命名文件夹。

因此,您应该避免将aspx页面放在App_Code文件夹中,因为它令人困惑,在Web应用程序项目中没有任何意义。为此使用更好的命名文件夹。出于这个原因,我将不会发布一个解决方案来使其工作,因为你不应该首先这样做。

相反,将App_Code文件夹重命名为有意义的文件,比如API,然后在你的Ajax中使用该名称,如/api


0
投票

你好在ajax调用尝试更改URL如:url:'App_Code / Index.aspx / Page_Load'我不确定它是否是答案但是试试看。

© www.soinside.com 2019 - 2024. All rights reserved.