关于定义的端点参数获得元数据的最通用方法是什么?

发布于 2025-02-07 11:30:43 字数 780 浏览 3 评论 0原文

我们可以定义某些端点参数应从查询字符串绑定:[fromquery(name =“ SomeParamName”)]

此映射信息是否存储在端点元数据中?

我想确保为端点定义传递的查询字符串参数

[HttpGet("foo")]
public IActionResult Foo([FromQuery("foo-param")]string param)
{
    ...
} 

[HttpGet("bar")]
public IActionResult Bar()
{
    ...
}

。代码>是一个预期的 但是,对于/bar?foo-param =某物 url foo-param是外国的。

对于控制器端点,我可以找到ContranterActionDescriptor元数据条目,该条目具有参数带有bindingInfo包含所有我需要的数据的收集。

但是,对于映射的端点,我仅获得请求委托信息。

当然,我可以从委托的target方法中获取parameterInfo对象,然后检查[FORFORQUERY]属性等等,依此类推,但这是一个相当的许多低级样板代码。 我还看到,端点Sapiexplorer扩展了此信息,并宣传生成了正确的文档。因此,我认为Sohuld是获取有关端点参数的信息的通用方法。

We can define that some of the endpoint parameters should be bound from query string: [FromQuery(Name= "someParamName")].

Is this mapping information stored anywhere in endpoint metadata?

I want to ensure that passed query string parameter is defined for the endpoint.

[HttpGet("foo")]
public IActionResult Foo([FromQuery("foo-param")]string param)
{
    ...
} 

[HttpGet("bar")]
public IActionResult Bar()
{
    ...
}

Here /foo?foo-param=something is fine, because foo-param is an expected one,
but for /bar?foo-param=something url foo-param is foreign.

For controller endpoints I could locate ControllerActionDescriptor metadata entry, which has Parameters collection with BindingInfo containing all the data I need.

However, for mapped endpoints I get only request delegate information.

Sure, I could get ParameterInfo objects from delegate's Target method, and then check for [FromQuery] attribute, and so on, but it's quite a lot of low-level boilerplate code.
I also see that EndpointsApiExplorer extacts this information and swagger generated correct documentation. Thus I assume there sohuld be a universal way to get the information about endpoint's parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文