ASP.NET Web Api returns 200 OK 当它应该 return 404

ASP.NET Web Api returns 200 OK when it should return 404

我在 ASP.NET Web API 项目的控制器上有以下操作方法:

[Route("api/v2/project/{projectId}/stuff"), HttpGet]
public IHttpActionResult Get(int projectId)

[Route("api/v2/project/{projectId}/stuff/{id:guid}"), HttpGet]
public IHttpActionResult Get(int projectId, [FromUri] Guid id)

[Route("api/v2/project/{projectId}/stuff"), HttpPost]
public IHttpActionResult Post(int projectId, [Required] Stuff stuff)

[Route("api/v2/project/{projectId}/stuff/{id:guid}"), HttpPut]
public IHttpActionResult Put(int projectId, [FromUri] Guid blastId, Stuff stuff)

[Route("api/v2/project/{projectId}/stuff/{id:guid}"), HttpDelete]
public IHttpActionResult Delete(int projectId, [FromUri] Guid id)

由于 javascript 错误,我向

发出了 DELETE 请求
api/v2/project/1234/stuff/undefined

即我得到的不是 GUID 作为 id,而是字符串 "undefined"。据我所知,这不应该匹配我的任何路线,但我得到的不是 404 Not found(甚至 405 Method not allowed),而是 200 OK 作为响应。

我在这些操作方法中的每一个中都设置了一个断点,并使用 Fiddler 重复了请求,但是 none 个断点被命中了。我还尝试从 nuget 安装 WebApiRouteDebugger 包,但我们使用的是自定义控制器工厂,它通过我们的 DI 容器连接起来,所以我根本无法让它工作。我什至尝试从我的全球注册过滤器之一抛出以下异常:

throw new Exception(actionContext.ControllerContext.ControllerDescriptor.ControllerName +
" " + actionContext.ActionDescriptor.ActionName);

但是 DELETE 请求 仍然 到达 200 OK(没有对有效网址的请求似乎可以做到这一点)。

我还能如何解决这个问题?根本原因可能是什么?

protected void Application_Start(object sender, EventArgs e) 所在的 Global.asax.cs 文件中,添加以下内容:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
    }

所有服务器请求都应该通过这里。

如果没有则添加这些。

using System.Web.Compilation;
using System.Reflection;

然后在开始请求调用中添加此代码以列出所有活动路由。

        string Items = "";
        IEnumerable<Assembly> Assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

        foreach (Assembly FoundAssembly in Assemblies)
        {
            string AssemblyName = FoundAssembly.FullName;
            IEnumerable<TypeInfo> Types = FoundAssembly.DefinedTypes.Where(type => type != null && type.IsPublic && type.IsClass && !type.IsAbstract && typeof(ApiController).IsAssignableFrom(type));
            foreach (TypeInfo ControllerType in Types)
            {
                System.Web.Http.Controllers.ApiControllerActionSelector ApiControllerSelection = new System.Web.Http.Controllers.ApiControllerActionSelector();
                System.Web.Http.Controllers.HttpControllerDescriptor ApiDescriptor = new System.Web.Http.Controllers.HttpControllerDescriptor(new System.Web.Http.HttpConfiguration(), ControllerType.Name, ControllerType);
                ILookup<string, System.Web.Http.Controllers.HttpActionDescriptor> ApiMappings = ApiControllerSelection.GetActionMapping(ApiDescriptor);

                foreach (var Maps in ApiMappings)
                {
                    foreach (System.Web.Http.Controllers.HttpActionDescriptor Actions in Maps)
                    {
                        Items += "[ controller=" + ControllerType.Name + " action=" + ((System.Web.Http.Controllers.ReflectedHttpActionDescriptor)(Actions)).MethodInfo + "]";
                    }
                }
            }
        }

这将列出所有控制器及其签名。如果您的 URL 不适合其中任何一个,您可能需要扩展列表以包括非控制器路线。

我不确定您的路由,但我认为您的路由不包含映射中的操作:

api/v2/project/1234/stuff/undefined 

为此检查网络 api 配置:api/{controller}[/{action}]/{id}。我想你可能会用你的动作点击另一个路由动作(比如 GET)。

P.S。 post 您要更新答案的路由配置。

我遇到了同样的问题。在我的 startup.cs 中,我有方法

app.Run(async context =>
       {
           context.Response.ContentType = "text/plain";
           await context.Response.WriteAsync("My Api");
       });

这导致所有路由都收到 200 响应以及字符串。 我只想在启动时显示此响应。这就是解决方案。

 app.MapWhen(ctx => ctx.Request.Path.Value == "/", StartupPage);

与同一 Startup 中的方法一起 class

  private void StartupPage(IAppBuilder app)
    {
        app.Run(async (context) =>
        {
            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync("My Api");
        });
    }

当在基地 url 上发出请求时,我只 return 200 OK 消息。