ASP.NET Web Api在返回404时返回200 OK

我在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

即,而不是ID的GUID,我得到字符串“undefined”.据我所知,这不应该与我的任何路线相匹配,但是不是找不到404(甚至不允许405方法),我得到了200 OK作为回应.

我在每个动作方法中设置了一个断点,并使用fiddler重复了请求,但没有一个断点被击中.我也尝试从nuget安装WebApiRouteDebugger软件包,但我们正在使用一个自定义控制器工厂,它通过我的DI容器挂钩,所以我根本无法使用它.我甚至尝试从我的全球注册过滤器中抛出以下异常:

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

但DELETE请求仍然可以达到200 OK(没有请求有效网址似乎这样做).

我还能怎么解决这个问题呢?可能是根本原因?

解决方法

在受保护的void Application_Start(对象发送方,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不适合其中任何一个,则可能必须展开列表以包含非控制器路由.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....