简体   繁体   English

是否可以在FromURI和FromBody上创建参数绑定?

[英]Is it possible to create a parameter binding on both FromURI and FromBody?

I looked up up the documentation for ASP.NET Web API parameter binding, they seem to have either fromURI and fromBody only. 我查找了ASP.NET Web API参数绑定的文档,它们似乎仅具有fromURI和fromBody。 Is it possible to do both? 两者都可以吗?

Here is some background info. 这是一些背景信息。 I'm creating a webhook receiver/handler, where I have control over which URL is the webhook, but I do not have control over what the payload will be like until later stage of the workflow, so I need to take it in as JSON string first. 我正在创建一个Webhook接收器/处理程序,在这里我可以控制哪个URL是Webhook,但是直到工作流的后期,我都无法控制有效负载的样子,因此我需要将其作为JSON接收字符串优先。

My hope is to be able to set up the route that can take in querystring and also Json string payload from HTTP POST. 我希望能够设置可以从HTTP POST接收querystring以及Json字符串有效负载的路由。 For example .../api/incoming?source=A. 例如... / api / incoming?source = A。

If I understand correctly you're trying to use both the Post data from the body and some parameters from the URI. 如果我理解正确,则您尝试同时使用正文中的Post数据和URI中的某些参数。 The example below should capture your "source=a" value from the queryString. 下面的示例应从queryString捕获您的“ source = a”值。

    [Route("incoming")]
    [HttpPost]
    public IHttpActionResult Test([FromBody] string data, string source)
    {
        //Do something

        return Ok("my return value");
    }

Or you could use as below if you formatted your route as .../api/incoming/source/A. 或者,如果将路由格式设置为... / api / incoming / source / A,则可以按以下方式使用。

    [Route("incoming/{source:string}")]
    [HttpPost]
    public IHttpActionResult Test([FromBody] string data, string source)
    {
        //Do something

        return Ok("my return value");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM