的ASP.NET Web API返回200 OK时,它应该返回404 [英] ASP.NET Web Api returns 200 OK when it should return 404

查看:649
本文介绍了的ASP.NET Web API返回200 OK时,它应该返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following action methods on a controller in an ASP.NET Web API project:

[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错误,我做了一个删除要求

api/v2/project/1234/stuff/undefined

即。而不是 GUID 的ID,我得到的字符串未定义。据我所知,这不应该与任何我的路线,但不是一个 404未找​​到(甚至不允许的405方法),我得到了一个 200 OK 作为响应。

i.e. instead of a GUID for the id, I got the string "undefined". As far as I can tell, this shouldn't match any of my routes, but instead of a 404 Not found (or even 405 Method not allowed), I got a 200 OK as response.

我将在每一个上述的操作方法断点并重复使用招的要求,但没有一个断点被击中。我也尝试从的NuGet安装包WebApiRouteDebugger,但我们正在使用自定义控制器工厂,通过我们的DI容器挂钩的东西,所以我不能让它在所有的工作。我甚至尝试从我的全局注册过滤器之一抛出以下异常:

I set a breakpoint in each of these action methods and repeated the request using Fiddler, but none of the breakpoints was hit. I also tried installing the WebApiRouteDebugger package from nuget, but we're using a custom controller factory which hooks things up through our DI container, so I couldn't get it to work at all. I even tried throwing the following exception from one of my globally registered filters:

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

删除要求仍然通过去 200 OK (无要求有效的网址似乎这样做)。

but the DELETE request still goes through to 200 OK (no requests to valid urls seem to do that).

我还能如何解决呢?可能是什么病根?

How else can I troubleshoot this? What could be the root cause?

推荐答案

在你的Global.asax.cs文件在您保护无效的Application_Start(对象发件人,EventArgs五)是,添加以下内容:

In your Global.asax.cs file where your protected void Application_Start(object sender, EventArgs e) is, add the following:

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

所有服务器的请求应该经过这里。

All server requests should come through here.

这些使用,如果不存在添加。

Add these using if not there.

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

然后在开始请求调用添加此code,列出所有可用的路由。

Then in the begin request call add this code to list out all of the active routes.

        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 + "]";
                    }
                }
            }
        }

这将列出所有控制器和他们的签名。如果您的网址不适合任何这些你可能要展开列表,包括非控制器的路线。

This will list all controllers and their signatures. If your URL does not fit into any of these you may have to expand the list to include non controller routes.

这篇关于的ASP.NET Web API返回200 OK时,它应该返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆