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不适合其中任何一个,则可能必须展开列表以包含非控制器路由. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET MVC和FubuMVC之间的主要区别是什么?
- asp.net-mvc – 相同的项目解决方案或同一解决方案中的新项
- 两个单选按钮ASP.NET C#
- 在ASP.NET网站中使用SQL Server Compact Edition
- 如何从ASP .NET网站检测客户端上安装的Java运行时?
- asp.net-mvc – 如何将httppostedfilebase转换为String数组
- asp.net-mvc – 传递给Html.ActionLink时在模型上序列化ILi
- asp.net-mvc – 使用MVC3 WebGrid助手向html属性名称添加连
- asp.net-mvc – 在WCF项目中设置Web API
- asp.net-mvc – 将隐藏文本字段附加到表单MVC
推荐文章
站长推荐
- asp.net-mvc – 该字符串未被识别为有效的布尔字
- asp.net-mvc – 与MVC一起使用的学习实体框架,首
- asp.net-mvc – 我一直在使用MVC3获取HTML5语法错
- asp.net-mvc – Umbraco 7自定义cookie
- asp.net-mvc – Razor视图引擎优于其他视图引擎的
- asp.net-mvc – MVC简单模型绑定无法正常工作
- asp.net-mvc – ASP.NET MVC应用程序性能分析
- 使用Azure ACS时无法从Active Directory注销
- asp.net – 注册.NET 4.5 IIS 10 Windows 10
- ASP.NET会话状态InProc
热点阅读