是否可以在FromURI和FromBody上创建参数绑定? [英] Is it possible to create a parameter binding on both FromURI and FromBody?

查看:60
本文介绍了是否可以在FromURI和FromBody上创建参数绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查找了ASP.NET Web API参数绑定的文档,它们似乎仅具有fromURI和fromBody.可以两者都做吗?

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?

以下是一些背景信息.我正在创建一个Webhook接收器/处理程序,在这里我可以控制哪个URL是Webhook,但是直到工作流的后期,我都无法控制有效负载的样子,因此我需要将其作为JSON接收首先输入字符串.

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.

我的希望是能够设置可以接受来自HTTP POST的querystring以及Json字符串有效负载的路由.例如.../api/incoming?source = A.

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.

推荐答案

如果我正确理解,您正在尝试同时使用正文中的Post数据和URI中的某些参数.下面的示例应从queryString捕获您的"source = a"值.

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");
    }

或者,如果您将路线的格式设置为.../api/incoming/source/A,则可以按以下方式使用.

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");
    }

这篇关于是否可以在FromURI和FromBody上创建参数绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆