ASP.NET的Web API无法获得应用程序/ x-WWW的形式urlen codeD HTTP POST [英] ASP.NET web api cannot get application/x-www-form-urlencoded HTTP POST

查看:263
本文介绍了ASP.NET的Web API无法获得应用程序/ x-WWW的形式urlen codeD HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Web API。我想用网络API接收HTTP POST数据。内容类型为应用程序/ x-WWW的形式urlen codeD ,并请求体就像是:

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:

数据= {陆委会:0004ED123456,模式:SG6200NXL} (JSON格式)。

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

我的控制器是这样的:

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

但FORMDATA总是空。只有当我请求主体更改为:

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

= {陆委会:0004ED123456,模式:SG6200NXL}

我可以找到 {陆委会:0004ED123456,模式: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:

数据= {陆委会:0004ED123456,模式: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"}

我必须使用内容类型:应用程序/ x-WWW的形式urlen codeD 据我所知,因为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.

推荐答案

从<一个报价href=\"http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1#sending_form_data_via_ajax\">here:

在默认情况下,网络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.

网页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.

所以,如果你想要的格式发布数据数据=串,你必须创建复杂类型。

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无法获得应用程序/ x-WWW的形式urlen codeD HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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