如何在asp.net上反序列化来自EXT.NET的JSON请求.. NET Web Api端
本文关键字:NET JSON 请求 Web Api EXT asp net 反序列化 | 更新日期: 2023-09-27 18:18:08
我正在尝试使用EXT.NET库从web客户端制作HTTPPOST。
这是我的调用的样子
<ext:Button ID="dugmeOdobri" runat="server" Text="Odobri Narudžbe" Icon="Accept" >
<DirectEvents>
<Click
Success="sayHello(result);"
Url="http://dnndev.me:9010/DesktopModules/WebApiSpn/API/odobrenje/odobri"
Type="Load"
Method="POST"
CleanRequest="true"
Json="true" >
<ExtraParams>
<ext:Parameter Name="Values" Value="Ext.encode(#{DocumentsGrid}.getRowsValues({selectedOnly:true}))" Mode="Raw" />
</ExtraParams>
</Click>
</DirectEvents>
</ext:Button>
Fiddler and Chromes的F11捕获调用像这样
{"Values":"[{'"ID'":6894443,
'"SkladisteSifra'":'"001'",
'"SkladisteNaziv'":'"Skladi''u0161te Veleprodaje RGT'",
'"StatusCode'":null,
'"StatusDescription'":'"Aktivna'",
'"Broj'":'"000009'",
'"Datum'":'"2013-06-13T00:00:00'",
'"PartnerSifra'":'"24055'",
'"PartnerNaziv'":'"15 APRIL'",
'"Napomena'":'"UGALJ'",
'"TipDokumenta'":'"VEL - Narud''u017eba dobavlja''u010du-slikovnica '",
'"Odobriti'":true,
'"Editable'":true}
]"}
当我粘贴字符串到json2sharp我得到这样的poco:
public class RootObject
{
public string Values { get; set; }
}
我想要的是像这样反序列化我对poco的请求:
public class RootObject
{
public int ID { get; set; }
public string SkladisteSifra { get; set; }
public string SkladisteNaziv { get; set; }
public object StatusCode { get; set; }
public string StatusDescription { get; set; }
public string Broj { get; set; }
public string Datum { get; set; }
public string PartnerSifra { get; set; }
public string PartnerNaziv { get; set; }
public string Napomena { get; set; }
public string TipDokumenta { get; set; }
public bool Odobriti { get; set; }
public bool Editable { get; set; }
}
应该如何看起来像方法在服务器端?在过去的3个小时里,我尝试了n种组合中的一种
[HttpPost]
[AllowAnonymous]
public string odobri(HttpRequestMessage request)
{
Dictionary<string,string> values = request.Content.ReadAsAsync<Dictionary<string, string>>().Result;
narudzbeZaOdobriti c = null; ;
foreach (string s in values.Values)
{
c = Newtonsoft.Json.JsonConvert.DeserializeObject<narudzbeZaOdobriti>(s);
break;
}
return c.Broj;
}
我不能得到我做错的地方,是我对Web Api的调用缺少一些东西吗?
是否强制使用HttpRequestMessage?
这里我准备了一个MVC的例子。
<<p> 视图/strong><%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Store store = this.GridPanel1.GetStore();
store.DataSource = new object[]
{
new SomeEntity()
{
Test1 = "test11",
Test2 = "test12",
Test3 = "test13"
},
new SomeEntity()
{
Test1 = "test21",
Test2 = "test22",
Test3 = "test23"
},
new SomeEntity()
{
Test1 = "test31",
Test2 = "test32",
Test3 = "test33"
}
};
}
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:Button runat="server" Text="Submit selected rows" Icon="Accept">
<DirectEvents>
<Click
Success="alert(Ext.encode(result));"
Url="/Aspx/SubmitSelected"
Method="POST">
<ExtraParams>
<ext:Parameter
Name="values"
Value="Ext.encode(#{GridPanel1}.getRowsValues({ selectedOnly:true }))"
Mode="Raw" />
</ExtraParams>
</Click>
</DirectEvents>
</ext:Button>
<ext:GridPanel ID="GridPanel1" runat="server">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="Test1" />
<ext:ModelField Name="Test2" />
<ext:ModelField Name="Test3" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:Column runat="server" Text="Test1" DataIndex="Test1" />
<ext:Column runat="server" Text="Test2" DataIndex="Test2" />
<ext:Column runat="server" Text="Test3" DataIndex="Test3" />
</Columns>
</ColumnModel>
<SelectionModel>
<ext:RowSelectionModel runat="server" SelectedIndex="1" Mode="Simple" />
</SelectionModel>
</ext:GridPanel>
</body>
</html>
控制器using System.Collections.Generic;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
namespace Work2MVC.Controllers
{
public class AspxController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult SubmitSelected(string values)
{
List<SomeEntity> data = JSON.Deserialize<List<SomeEntity>>(values);
return this.Direct(data.Count);
}
}
}
SomeEntity.cs
public class SomeEntity
{
public string Test1 { get; set; }
public string Test2 { get; set; }
public string Test3 { get; set; }
}
您可以简单地尝试:
public string odobri(HttpRequestMessage request)
{
Dictionary<string, string> values = request.Content.ReadAsAsync<Dictionary<string, string>>().Result;
RootObject c = null;
foreach (string s in values.Values)
{
List<RootObject> tmp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(s);
c = tmp.First();
break;
}
return c.Broj;
}