删除内部“__type” ASP.NET Restful WCF 中的标签

发布于 2024-12-07 05:25:37 字数 2264 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔芯 2024-12-14 05:25:37

好的,修复了它 - 但最终做的事情有点不同。 WCF 似乎坚持在序列化字典时放入这些内部“__type”标记,但由于某种原因,它不会对 Streams 执行相同的操作。

这是新的 Web 服务方法代码(只是返回类型发生了变化):

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Stream Test1(String Token, String StreamId)
{
    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);
}

这是新的 SuccessResult 函数(这就是造成差异的原因):

private Stream SuccessfulResult(DataTable dt = null)
{
    Dictionary<string, object> returnDict = new Dictionary<string, object>();
    returnDict.Add("Status", "Success");

    Dictionary<string,object> dct = new Dictionary<string,object>();
    foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
        dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());

    returnDict.Add("Data", dct);

    string sResponse = json.Serialize(returnDict);
    byte[] byResponse = Encoding.UTF8.GetBytes(sResponse);

    return new MemoryStream(byResponse);
}

现在输出看起来完全一样我希望它看起来:

{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}

无论如何,我希望这个例子对其他人有帮助:)

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):

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Stream Test1(String Token, String StreamId)
{
    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);
}

And here's the new SuccessfulResult function (which is what made the difference):

private Stream SuccessfulResult(DataTable dt = null)
{
    Dictionary<string, object> returnDict = new Dictionary<string, object>();
    returnDict.Add("Status", "Success");

    Dictionary<string,object> dct = new Dictionary<string,object>();
    foreach (DataColumn currCol in dt.Rows[0].Table.Columns)
        dct.Add(currCol.ColumnName, dt.Rows[0][currCol.ColumnName].ToString());

    returnDict.Add("Data", dct);

    string sResponse = json.Serialize(returnDict);
    byte[] byResponse = Encoding.UTF8.GetBytes(sResponse);

    return new MemoryStream(byResponse);
}

Now the output looks exactly how I want it to look:

{"Status":"Success","Data":{"Key1":"Value1","Key2":"Value2","Key3":"Value3"}}

Anyway, I hope this example helps somebody else :)

淡淡绿茶香 2024-12-14 05:25:37

在某些情况下,浏览器会将响应流视为二进制,因此,它将显示下载文件弹出窗口。
那么你应该使用:

string result = "Hello world";
byte[] resultBytes = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new MemoryStream(resultBytes);

In some case, the browser will treat response stream as binary, so, it will show download file popup.
Then you should using:

string result = "Hello world";
byte[] resultBytes = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new MemoryStream(resultBytes);
西瑶 2024-12-14 05:25:37

我知道这是很久以前的事了,但我找到了一种非常简单的方法来实现这一点。

在 Web.Config 文件中,您应该更改一行。

使用 而不是

  <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>

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/>

  <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文