.NET的WebAPI看到原始的请求 [英] .NET WebApi see raw request

查看:145
本文介绍了.NET的WebAPI看到原始的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

public void Post([FromBody]Record value)

-method,我可以从一些JSON小提琴手调用。

-method that I can call from fiddler with some json.

在调用使用我的Mac WizTools RESTClient实现3.1具有相同的JSON的方法,在总是 。它看起来像它不会解析什么的。

When calling the method from my mac using WizTools RESTClient 3.1 with the same json, the value is always null. It looks like it does not get parsed or something.

我用内容类型:应用程序/ JSON 在两台机器上,我有双重检查的 Request.Content 对象在Visual Studio调试器的标头。

I'm using Content-Type: application/json on both machines and I have double-checked that the Request.Content object the headers in the Visual Studio debugger.

如果我用一个简单的对象,只有2个属性,像这样:

If I use a simpler object with only 2 properties like so:

public class Test123 {
  public string name { get; set; }
  public int age { get; set; }
}

public void Post([FromBody]Test123 value)

我可以从两个小提琴手和MAC和调用它不能为null。

I can call it from both fiddler and the mac and the value is never null.

那么如何调试这个任何提示?是否有实例的任何办法,我看到我的Mac发送到IIS在我​​的电脑/ Visual Studio中的原始反应?它不会在小提琴手露面。

So any tips on how to debug this? Is there for instance any way for me to see the raw response sent from my mac to iis/visual studio on my PC? It does not show up in fiddler.

推荐答案

有关您的操作方法,如果你发送带有内容类型的请求:应用程序/ JSON 的请求消息体{名:约翰,时代:20} ,它应该正确绑定。顺便说一句,你并不需要使用 [FromBody] ,因为 Test123 是一个复杂类型。

For your action method, if you send a request with Content-Type: application/json and the request message body of {"name":"john", "age":20}, it should correctly bind. BTW, you do not need to use [FromBody], since Test123 is a complex type.

不管怎么说,你可以通过添加一个消息处理程序像这样查看原始请求消息。

Anyways, you can view the raw request message by adding a message handler like this.

public class MyHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
                                             HttpRequestMessage request,
                                                CancellationToken token)
    {
        HttpMessageContent requestContent = new HttpMessageContent(request);
        string requestMessage = requestContent.ReadAsStringAsync().Result;

        return await base.SendAsync(request, token);
    }
}

添加这样的处理程序 config.MessageHandlers.Add(MyHandler的新()); WebApiConfig.cs 注册方法。原始的请求将在 requestMessage 变量。您可以通过打破而存在调试或写入跟踪等检查。

Add the handler like this config.MessageHandlers.Add(new MyHandler()); in WebApiConfig.cs Register method. Raw request will be in requestMessage variable. You can inspect by breaking there while debugging or write to a trace, etc.

这篇关于.NET的WebAPI看到原始的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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