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

Tom*_*han 7 asp.net routing asp.net-web-api

我在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)
Run Code Online (Sandbox Code Playgroud)

由于javascript错误,我提出了DELETE请求

api/v2/project/1234/stuff/undefined
Run Code Online (Sandbox Code Playgroud)

即,而不是一个GUIDid,我得到了字符串"undefined".据我所知,这不应该与我的任何路线相匹配,但是我得到了一个响应而不是404 Not found(甚至405 Method not allowed)200 OK.

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

throw new Exception(actionContext.ControllerContext.ControllerDescriptor.ControllerName +
" " + actionContext.ActionDescriptor.ActionName);
Run Code Online (Sandbox Code Playgroud)

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

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

Bri*_*arm 3

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

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
    }
Run Code Online (Sandbox Code Playgroud)

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

使用 if not there 添加这些。

using System.Web.Compilation;
using System.Reflection;
Run Code Online (Sandbox Code Playgroud)

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

        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 + "]";
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

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