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

六眼飞鱼酱① 提交于 2019-12-31 06:03: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");
    }


来源:https://stackoverflow.com/questions/35160978/is-it-possible-to-create-a-parameter-binding-on-both-fromuri-and-frombody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!