使用contentType将Json字符串传递给Web API:'application / x-www-form-urlencoded' [英] Pass Json string to Web API with contentType: 'application/x-www-form-urlencoded'

查看:436
本文介绍了使用contentType将Json字符串传递给Web API:'application / x-www-form-urlencoded'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试实现一个方法,该方法将包含contentType:'application / x-www-form-urlencoded',这将是期待json字符串作为输入。



样本Json请求 -

{

'金额':'2500.00',

'买方':'foo@example.com',

'dequiren_name':'John Doe',

'deverly_phone':' 9999999999',

'货币':'INR',

'费用':'125.00',

'longurl':'https: //www.instamojo.com/@dalan/264a89803cec4401862f01e28f720eca',

'mac':'1ddf3b78f84d071324c0bf1d3f095898267d60ee',

'oayment_id':'MOJO5a06005J21512197',

'payment_request_id':'264a89803cec4401862f01e28f720eca',

'目的':'FIFA 16',

'shorturl':'https://imjo.in/ NNNXn',

'状态':'信用'

}



目前我还有e实现了对application / json完美正常的方法,但它不适用于contentType:'application / x-www-form-urlencoded'

我想让它适用于contentType:'application / x-www-form-urlencoded'



任何人都可以提供相同的指示

谢谢

Hello,

I'm trying to implement a method which will have contentType: 'application/x-www-form-urlencoded' which will be expecting json string as an input.

Sample Json request -
{
'amount': '2500.00',
'buyer': 'foo@example.com',
'buyer_name': 'John Doe',
'buyer_phone': '9999999999',
'currency': 'INR',
'fees': '125.00',
'longurl': 'https://www.instamojo.com/@dalan/264a89803cec4401862f01e28f720eca',
'mac': '1ddf3b78f84d071324c0bf1d3f095898267d60ee',
'payment_id': 'MOJO5a06005J21512197',
'payment_request_id': '264a89803cec4401862f01e28f720eca',
'purpose': 'FIFA 16',
'shorturl': 'https://imjo.in/NNNXn',
'status': 'Credit'
}

Currently I have implemented the method which perfectly works fine for application/json but it is not working for contentType: 'application/x-www-form-urlencoded'
I want to make this workable for contentType: 'application/x-www-form-urlencoded'

Could anyone please provide any pointers for the same
Thank you

推荐答案

当您将contentType用作application / x-www-form-urlencoded时,它会将附加值附加到url。这是contentType的默认值。它看起来像:

When you use contentType as "application/x-www-form-urlencoded", it appends append value to url. This is the default value of contentType. It looks like:
?Name=Manas&Age=28



但是当你使用contentType作为application / json时,它看起来像


But when you use contentType as "application/json", it looks like

{ Name : 'Manas', Age: 28}



因此application / x-www-form-urlencoded与application / json不同。



尝试使用以下代码:


So "application/x-www-form-urlencoded" is not same as "application/json".

Try with below code:

public class MyCustomClass {
    public string amount{ get; set; }
    public string buyer{ get; set; }
}



下一步使用.net JavaScriptSerializer()。反序列化以获取数据.Net自定义对象。


Next use .net JavaScriptSerializer().Deserialize to get data .Net custom object.

[HttpPost]
public void Post([FromBody]string formData){
    MyCustomClass obj = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<mycustomclass>(formData);
}


这篇关于使用contentType将Json字符串传递给Web API:'application / x-www-form-urlencoded'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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