Is it possible to create a parameter binding on bo

2020-05-01 09:19发布

问题:

I looked up up the documentation for ASP.NET Web API parameter binding, they seem to have either fromURI and fromBody only. 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.

My hope is to be able to set up the route that can take in querystring and also Json string payload from HTTP POST. For example .../api/incoming?source=A.

回答1:

If I understand correctly you're trying to use both the Post data from the body and some parameters from the URI. The example below should capture your "source=a" value from the queryString.

    [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.

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

        return Ok("my return value");
    }