ASP.NET web api 无法获取 application/x-www-form-urlencoded HTTP POST [英] ASP.NET web api cannot get application/x-www-form-urlencoded HTTP POST

查看:43
本文介绍了ASP.NET web api 无法获取 application/x-www-form-urlencoded HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 web-api 的新手.我想使用 web-api 接收 HTTP POST 数据.内容类型为application/x-www-form-urlencoded,请求正文如下:

I'm new to web-api. I want to receive a HTTP POST data using web-api. The content-type is application/x-www-form-urlencoded, and the request body is like:

data={"mac":"0004ED123456","model":"SG6200NXL"}(JSON 格式).

我的控制器是这样的:

public void Post([FromBody]string formData)
{
    data.Add(formData);
}

但 formData 始终为空.仅当我将请求正文更改为:

But formData is always null. Only when I change the request body to:

={"mac":"0004ED123456","model":"SG6200NXL"}

我可以找到 {"mac":"0004ED123456","model":"SG6200NXL"} 保存在我的 formData 字符串中.

I can find {"mac":"0004ED123456","model":"SG6200NXL"} was saved in my formData string.

所以我的问题是我应该如何接收以下格式的数据:

So my question is how should I receive the data with format:

data={"mac":"0004ED123456","model":"SG6200NXL"}?

是否有一种简单的方法可以将 JSON 淡化为 C#?

And is there a easy way that I can desalinize the JSON into C#?

感谢您的帮助!

更新:我尝试使用模型,但它仍然不适合我.我的模型是:

UPDATE: I tried to use model, but it still not work for me. My model is:

public class Device
    {
        public string mac { get; set; }
        public string model { get; set; }
    }

我的 HTTP POST 请求是:

and my HTTP POST request is:

标题:

User-Agent: Fiddler
Content-type: application/x-www-form-urlencoded
Host: localhost:52154
Content-Length: 46

正文:

data={"mac":"0004ED123456","model":"SG6200NX"}

据我所知,我必须使用 Content-type: application/x-www-form-urlencoded 因为 HTTP POST 是由路由器发送的.我的工作是接收数据.

I have to use Content-type: application/x-www-form-urlencoded as far as I know because the HTTP POST is sent by a router. My job is to receive the data.

推荐答案

引用自 这里:

默认情况下,Web API 会尝试从请求 URI 中获取简单类型.FromBody 属性告诉 Web API 从请求正文中读取值.

By default, Web API tries to get simple types from the request URI. The FromBody attribute tells Web API to read the value from the request body.

Web API 最多读取一次响应正文,因此请求正文中只能有一个动作参数.如果需要从请求正文中获取多个值,请定义复杂类型.

Web API reads the response body at most once, so only one parameter of an action can come from the request body. If you need to get multiple values from the request body, define a complex type.

其次,客户端需要发送如下格式的值:

Second, the client needs to send the value with the following format:

=价值

具体来说,名称/值对的名称部分对于简单类型必须为空.

Specifically, the name portion of the name/value pair must be empty for a simple type.

所以,如果你想以data=string格式发布数据,你必须创建复杂类型.

So, if you want to post data in the format data=string, you have to create complex type.

public class MyFormData
{
    public string Data { get; set; }
}

并像这样更新您的控制器:

And update your controller like so:

public void Post(MyFormData formData)
{
    //your JSON string will be in formData.Data
}

当然,您的其他替代方法是将内容类型更改为 JSON,但这实际上取决于您的要求.

Of course, other alternatives for you is to change the content type to JSON, but really depends on your requirements.

这篇关于ASP.NET web api 无法获取 application/x-www-form-urlencoded HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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