【问题标题】:404 Error from Web Forms When Posting to Web API Controller发布到 Web API 控制器时来自 Web 表单的 404 错误
【发布时间】:2018-10-17 11:06:42
【问题描述】:

我无法将 ASP.NET Web 表单中的表单数据发布到我的 Web API 控制器,因为我不断收到 404 或“未找到”错误。

这是我的 Global.asax.vb 文件中的代码:

Imports System.Web.Optimization
Imports System.Web.Http

Public Class Global_asax
    Inherits HttpApplication

    Sub Application_Start(sender As Object, e As EventArgs)
        ' Fires when the application is started
        RegisterRoutes(RouteTable.Routes)
        BundleConfig.RegisterBundles(BundleTable.Bundles)

        RouteTable.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", New With {.id = RouteParameter.Optional})

    End Sub
End Class

这是 Web 表单中的 AJAX:

$.ajax({
                type: 'POST',
                url: 'api/UserApi',
                data: $('form').serialize(),
                success: function(response) {
                    var result = JSON.parse(response);
                    if (result.validationError) {
                        $('.jumbotron').prepend('<div class="alert alert-danger"><strong>Make sure to fill in all fields.</strong></div>');

                        if ($('.alert-danger').length > 1) {
                            $('.alert-danger').last().remove();
                        }
                    }
                    else if (result.error) {
                        $('.jumbotron').prepend('<div class="alert alert-danger"><strong>User with that email address already exists!</strong></div>');

                        if ($('.alert-danger').length > 1) {
                            $('.alert-danger').last().remove();
                        }
                    }
                    else {
                        if ($('.alert-danger').is(':visible')) {
                            $('.alert-danger').remove();
                        }
                        $('.jumbotron h1').remove();
                        $('form').replaceWith('<div class="alert alert-success"><strong>User created successfully!</strong></div>');
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });

最后,这是 Web API 控制器中的代码:

Imports System.Net
Imports System.Web.Http
Imports System.Web.Http.Results

<RoutePrefix("api/UserApi")>
Public Class UserApiController
    Inherits ApiController

    ' POST api/<controller>
    Public Sub PostValue(<FromBody()> ByVal email As String, name As String)
        Dim user = New User With {
            .Email = email,
            .Name = name
        }

        Dim userDbContext = New UserDBContext()
        userDbContext.Users.Add(user)

    End Sub

End Class

控制器名为“UserApiController.vb”,位于项目的主目录中。我尝试将完全相同的路由放在 App_Start 目录中的 RouteConfig.vb 文件中,并使用您在控制器中的类声明上方看到的路由注释,仍然没有运气。另外,我将路由写为RouteTable.Routes.MapHttpRoute("DefaultApi", "api/{action}/{id}", New With {.controller = "UserApi", .id = RouteParameter.Optional}),并在 AJAX 中发布到 url api/PostValue。

【问题讨论】:

  • vb中有app_code目录吗?控制器应该在那里。 .aspx 页面的具体目录是什么?
  • @wazz 没有 app_code 目录,.aspx 文件在项目的主目录中。
  • 我对 web api 还很陌生,只设置了一个简单的 get。这就是我能建议的全部 - 设置一个简单的 getJson 直到对“未找到”问题进行排序。 $.getJSON("api/helloworld", function (data) { ....
  • @wazz 谢谢。我已将控制器放在建议的 app_code 文件夹中,现在我收到 500 错误而不是 404。现在我只需要弄清楚错误是什么。
  • 如果您有访问权限,Windows 日志可以帮助您处理 500 秒。开始 > 输入“事件查看器”。

标签: asp.net ajax vb.net asp.net-web-api webforms


【解决方案1】:

确保您的控制器位于名为 App_Code 的 ASP.NET 文件夹中。

Right-click website (or sim)
> Add
> Add ASP.NET Folder
> App_Code

所有 .cs 代码都应该在其中。 (不是页面的代码隐藏文件。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多