简体   繁体   English

C#将[FromBody]数据从一个控制器发布到另一个

[英]C# POSTing [FromBody] data from one controller to another

Is it possible to send [FromBody] POST data to a controller using client.GetAsync() (or PostAsync/SendAsync? 是否可以使用client.GetAsync() (或PostAsync / SendAsync client.GetAsync()[FromBody] POST数据发送到控制器?

I had to set up a base controller that all api calls will go through. 我必须设置一个基本控制器,所有api调用都将通过。

My ajax calls all go to this SecureApi controller, and they send the original path as a parameter to that they can be re-routed to the correct controller. 我的ajax调用全部转到此SecureApi控制器,并且它们将原始路径作为参数发送给它们,以便可以将其重新路由到正确的控制器。 Something like: 就像是:

$.ajax({
    url: "./api/SecureApi/?path=/api/OtherApi/SomeRoute",
    data: {
        param1: 1,
        param2: 2
    }
});

So my base controller looks something like: 所以我的基本控制器看起来像:

public class SecurityApiController : ApiController
{
    [HttpPost]
    public HttpResponseMessage SecureApi([FromBody]object data, string path)
    {
        // do some stuff

        // get uri
        var applicationPath = Request.RequestUri.Scheme + "://" + Request.GetRequestContext().VirtualPathRoot.Replace("/", String.Empty);
        Uri routeUri = new Uri(applicationPath + path);

        // then redirect to correct controller
        var config = new HttpConfiguration();
        var server = new HttpServer(config);
        var client = new HttpClient(server);
        // how can I send [FromBody]object data here?
        var response = client.GetAsync(routeUri).Result; // or PostAsync/SendAsync?
        return response;
    }
}

The other controller looks like: other控制器如下所示:

public class OtherApiController : ApiController
{
    [HttpPost]
    public HttpResponseMessage OtherApi([FromBody]OtherData data)
    {
        // do stuff
    }
}

Unfortunately I can't change OtherApi , so I HAVE to send the [FromBody] POST data in the same way (in the POST body). 不幸的是,我无法更改OtherApi ,所以我必须以相同的方式(在POST正文中)发送[FromBody] POST数据。

Is that possible? 那可能吗?

EDIT: 编辑:

Per @Philippe's response below, I'm using PostAsJsonAsync and it seems to want to work, but I'm getting a 401 Unauthorized result. 根据以下@Philippe的回复,我正在使用PostAsJsonAsync,它似乎想要工作,但是却收到401未经授权的结果。 More info: 更多信息:

I went with the correct(?) ASYNC/AWAIT route... 我选择了正确的(?)ASYNC / AWAIT路线...

public async Task<HttpResponseMessage> SecureApi([FromBody]Dictionary<string, dynamic> data, string path)
{
    ...
    var client = new HttpClient();
    var response = await client.PostAsJsonAsync(routePath, data);
    return response;
}

And the Other controller has: 并且Other控制器具有:

[Authorize(Roles = "Admin")] // I do have the "Admin" role
[Route("Save")]
[HttpPost]
public SaveResultBase Save([FromBody]Dictionary<string, dynamic> data)
{
    ...
}

But this controller is never hit (no breakpoints are hit there) and it returns a 401 Unauthorized response. 但是此控制器永远不会命中(那里没有断点),它会返回401未经授权的响应。

I guess that I have to add my user credentials to the client headers before calling PostAsJsonAsync . 我猜我必须在调用PostAsJsonAsync之前将用户凭据添加到客户端标头中。 Can't find any way to do that though. 虽然找不到任何方法。

The method GetAsync of HttpClient will send a HTTP GET request so it would only be possible to have [FromUri] arguments. HttpClient的GetAsync方法将发送HTTP GET请求,因此只能具有[FromUri]参数。 Because [FromBody] argument are by definition POST data, you will want to use PostAsJsonAsync / PostAsXmlAsync / PostAsync . 因为[FromBody]参数是定义上的POST数据,所以您将要使用PostAsJsonAsync / PostAsXmlAsync / PostAsync The difference between all of them is how the data is serialized. 它们之间的区别在于数据的序列化方式。

var response = client.PostAsJsonAsync(routeUri, data).Result;

That being said, if you have security in mind, it would be rather easy for anyone to call the "right api" directly. 话虽如此,如果您考虑到安全性,那么任何人都可以直接直接调用“正确的api”。 Moreover you will increase latency by generating two HTTP requests. 此外,您将通过生成两个HTTP请求来增加延迟。

You should take a look at this guide on MSDN. 您应该在MSDN上阅读本指南 I believe that an authentication filter is probably what you are looking for. 我相信身份验证筛选器可能是您要寻找的。

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

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