您能否在.NET WebApi中丢失查询导致错误的请求而不是404?

问题描述

我有以下控制器:

_framework/blazor.webassembly.js
css/app.css
css/bootstrap.min.css
img/myimg01.jpg

public class MyController { /// <summary>Retrieves all of the assets that match the given criteria.</summary> /// <param name="parentId">Id of parent entity.</param> /// <returns>A set of assets.</returns> [HttpGet] [Route("")] [SwaggerResponse(HttpStatusCode.OK,Type = typeof(MyModel))] public IHttpActionResult Get( int parentId,) { // ... return Ok(results); } } 是必需的,但是如果用户转到parentId,它将返回404。是否有一种方法可以拦截此响应并引发Bad Request,从而通知用户缺少的ID ?我尝试将parentId设置为可选,然后自己抛出错误的请求,但是这种方法与Swagger结合使用效果不佳,并把我引向了这个兔子洞:Can you mark an "optional" query as required in Swagger?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)