加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:04:04 所属栏目:asp.Net 来源:网络整理
导读:我在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
我在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不适合其中任何一个,则可能必须展开列表以包含非控制器路由.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读