处理邮件在控制器中不起作用 [英] Handling post is not working in controller

查看:82
本文介绍了处理邮件在控制器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Simples asp .net web api项目,默认控制器,最简单的POST处理程序:

Simples asp .net web api project, default controller, simplest possible POST handler:

[HttpPost]
        public void Post([FromBody]string value)
        {
            int a = 0;
        }





向邮递员发送简单的邮寄申请:



Sending simple post request with Postman to:

http://localhost:port/api/values





我只能发送application / json类型的原始数据 - 发送其他任何结果在控制器动作中根本没有执行,但无论我发送什么,处理程序中的



I can only send raw data of type application/json - sending anything else results in controller action not getting executed at all, but no matter what I am sending,

value

始终为null。有人可以指出我做错了吗?



我尝试过:



我已经尝试过,比如我所知道的一切。

in the handler is always null. Can someone point out what I am doing wrong?

What I have tried:

I have tried, like, everything that I'm aware of.

推荐答案

好的,我自己想出来了。要发送最简单的字符串,必须用引号括起来。它有多明显。
Ok, figured out it myself. To send a simplest possible string, it has to be wrapped in quotes. How obvious was it.


默认情况下,WebAPI仅绑定到JSON或XML请求(或您配置的任何其他媒体格式化程序支持的格式)。将字符串包装在引号中将有效地将其转换为JSON有效负载,但您需要注意编码问题,特别是如果您的字符串已包含引号。



如果您只想捕获原始请求正文,还有其他方法可以执行此操作:

使用ASP.NET Web API接受原始请求正文内容 - Rick Strahl的Web日志 [ ^ ]



最简单的选择可能是:

By default, WebAPI only binds to JSON or XML requests (or the formats supported by whatever other media formatters you have configured). Wrapping the string in quotes will effectively turn it into a JSON payload, but you'll need to watch out for encoding issues, particularly if your string already contains quotes.

If you just want to capture the raw request body, there are other ways to do that:
Accepting Raw Request Body Content with ASP.NET Web API - Rick Strahl's Web Log[^]

The simplest option is probably:
[HttpPost]
public async Task Post()
{
    string rawRequestBody = await Request.Content.ReadAsStringAsync();
    ...
}


这篇关于处理邮件在控制器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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