删除内部“__type” ASP.NET Restful WCF 中的标签
我正在开发一个 RESTful WCF,目前无法获得漂亮、干净的 JSON 序列化返回值。
不久前,我摆脱了使用包装器在每个键和值周围添加“Key”和“Value”标签(由序列化 Dictionary
对象引起)。
但是当我这样做时,“__type”标签开始出现。通过在 svc 文件的 ServiceHost 标记中将 WebScriptServiceHostFactory 替换为 WebServiceHostFactory,我设法摆脱了“d”标记以及第一个“__type”标记。
所以我的结果看起来像这样:
{"Status":"Success","Data":{"__type":"SerializableDictionaryOfstringanyType:#MyNamespace.MyFolder","Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
但我希望它看起来像这样:
{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
我的测试 Web 服务代码看起来像这样:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public SerializableDictionary<string, object> Test1(String Token, String Id)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Key1", typeof(System.String));
testTable.Columns.Add("Key2", typeof(System.String));
testTable.Columns.Add("Key3", typeof(System.String));
DataRow testRow = testTable.NewRow();
testRow["Key1"] = "Value1";
testRow["Key2"] = "Value2";
testRow["Key3"] = "Value3";
testTable.Rows.Add(testRow);
return SuccessfulResult(testTable);
}
编辑: 成功结果函数看起来像这样(抱歉忘记了):
private SerializableDictionary<string, object> SuccessfulResult(DataTable dt = null)
{
SerializableDictionary<string, object> result = new SerializableDictionary<string, object>();
result.Add("Status", "Success");
if (dt == null || dt.Rows.Count != 1)
return result;
SerializableDictionary<string, object> dct = new SerializableDictionary<string, object>();
foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());
result.Add("Data", dct);
return result;
}
如果有人有关于如何摆脱最后一个小“__type”标签的任何想法,我很想听听!非常感谢,如果我可以发布其他可能有帮助的内容,请告诉我。
I'm developing a RESTful WCF, and am currently having trouble getting nice, clean JSON-serialized return values.
A little while back, I got rid of "Key" and "Value" tags around each of my keys and values (caused by serializing a Dictionary<string, object>
object) by using a wrapper.
But when I did that, "__type" tags started showing up. I managed to get rid of the "d" tag, along with the first "__type" tag by replacing WebScriptServiceHostFactory with WebServiceHostFactory in the ServiceHost tag of my svc file.
So my result looks like this:
{"Status":"Success","Data":{"__type":"SerializableDictionaryOfstringanyType:#MyNamespace.MyFolder","Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
But I want it to look like this:
{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}
My test webservice code looks like this:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public SerializableDictionary<string, object> Test1(String Token, String Id)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Key1", typeof(System.String));
testTable.Columns.Add("Key2", typeof(System.String));
testTable.Columns.Add("Key3", typeof(System.String));
DataRow testRow = testTable.NewRow();
testRow["Key1"] = "Value1";
testRow["Key2"] = "Value2";
testRow["Key3"] = "Value3";
testTable.Rows.Add(testRow);
return SuccessfulResult(testTable);
}
EDIT: And the SuccessfulResult function looks like this (sorry for forgetting it):
private SerializableDictionary<string, object> SuccessfulResult(DataTable dt = null)
{
SerializableDictionary<string, object> result = new SerializableDictionary<string, object>();
result.Add("Status", "Success");
if (dt == null || dt.Rows.Count != 1)
return result;
SerializableDictionary<string, object> dct = new SerializableDictionary<string, object>();
foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());
result.Add("Data", dct);
return result;
}
If anybody has any ideas on how I might get rid of that last little "__type" tag, I would love to hear them! Thanks very much, and let me know if there's anything else I can post that might be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,修复了它 - 但最终做的事情有点不同。 WCF 似乎坚持在序列化字典时放入这些内部“__type”标记,但由于某种原因,它不会对 Streams 执行相同的操作。
这是新的 Web 服务方法代码(只是返回类型发生了变化):
这是新的 SuccessResult 函数(这就是造成差异的原因):
现在输出看起来完全一样我希望它看起来:
无论如何,我希望这个例子对其他人有帮助:)
Okay, fixed it up - but ended up doing things a little differently. WCF seems to insist on putting in those internal "__type" tags when it serializes Dictionaries, but for some reason, it doesn't do the same thing with Streams.
Here's the new webservice method code (just the return type has changed):
And here's the new SuccessfulResult function (which is what made the difference):
Now the output looks exactly how I want it to look:
Anyway, I hope this example helps somebody else :)
在某些情况下,浏览器会将响应流视为二进制,因此,它将显示下载文件弹出窗口。
那么你应该使用:
In some case, the browser will treat response stream as binary, so, it will show download file popup.
Then you should using:
我知道这是很久以前的事了,但我找到了一种非常简单的方法来实现这一点。
在 Web.Config 文件中,您应该更改一行。
使用
而不是I know that this was a long time ago, but I found a really simple way to make that possible.
In the Web.Config file you should change a line.
Use
<webHttp/>
instead of<enableWebScript/>