wcf - 动态返回类型xml或json [英] wcf - dynamic return type xml or json

查看:133
本文介绍了wcf - 动态返回类型xml或json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  如何制作wcf服务方法,该方法在中返回数据   short   如何启用wcf服务方法 return 动态输出xml  json。





i制作了一种符合我要求的方法,但我知道这不是真正的方法来做到这一点。



我的代码:



接口:



 [OperationContract] 
[WebInvoke(Method = GET ,BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = / ReturnTime?ReturnType = {ReturnType })]
< span class =code-keyword> string ReturnTime( String ReturnType);







方法:





 < span class =code-keyword> public   string  ReturnTime( String  ReturnType)
{
if (ReturnType!= && ReturnType!= null
{
if (ReturnType.ToUpper()= = STRING || ReturnType.ToUpper()== JSON || ReturnType.ToUpper()== XML
{
if (ReturnType.ToUpper()== JSON
return [{\当前时间为\: + DateTime.Now.ToString()+ }];
else if (ReturnType.ToUpper()== XML
return < CurrentTimeIs> + DateTime.Now.ToString()+ < / CurrentTimeIs>;
else if (ReturnType.ToUpper()== STRING
return 当前时间是: + DateTime.Now.ToString();
else
return < CurrentTimeIs> + DateTime.Now.ToString()+ < / CurrentTimeIs>中;
}
其他
{
返回 < ErrorMessage>< Message> + ReturnType + 由于返回类型不可用。请使用JSON,XML或STRING。< / Message>< / ErrorMessage>;
}
}
else
{
return < ErrorMessage>< Message>请提供返回类型JSON,XML或STRING。< / Message> ;< /&的ErrorMessage gt;中;
}
}





我这样做。我知道wcf有能力做到这一点,但它怎么做我不知道PLZ帮我实现这一目标。提前完成。

解决方案

你可以检查

http://blogs.msdn.com/b/dotnetinterop/archive/2008/ 11/04 / rest-in-wcf-varying-response-content-type-based-on-http-request-headers.aspx


http://www.kailashtandel.com/2013/12/callling-wcf-rest-service-from-cross。 html [ ^ ]

how to make wcf service method which returns the data in both xml and json,
in short how to enable wcf service method which return dynamic output either xml or json.



i made one method that meet my requirement but i know its not true method to do this.

my code:

INTERFACE :

[OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/ReturnTime?ReturnType={ReturnType}")]
        string ReturnTime(String ReturnType);




METHOD :


public string ReturnTime(String ReturnType)
        {
            if (ReturnType != "" && ReturnType != null)
            {
                if (ReturnType.ToUpper() == "STRING" || ReturnType.ToUpper() == "JSON" || ReturnType.ToUpper() == "XML")
                {
                    if (ReturnType.ToUpper() == "JSON")
                        return "[{\"Current Time is \":" + DateTime.Now.ToString() + "}]";
                    else if (ReturnType.ToUpper() == "XML")
                        return "<CurrentTimeIs>" + DateTime.Now.ToString() + "</CurrentTimeIs>";
                    else if (ReturnType.ToUpper() == "STRING")
                        return "Current time is : " + DateTime.Now.ToString();
                    else
                        return "<CurrentTimeIs>" + DateTime.Now.ToString() + "</CurrentTimeIs>";
                }
                else
                {
                    return "<ErrorMessage><Message>" + ReturnType + " as returntype is not available. Please use JSON,XML OR STRING.</Message></ErrorMessage>";
                }
            }
            else
            {
                return "<ErrorMessage><Message>Please provide Returntype either JSON,XML OR STRING.</Message></ErrorMessage>";
            }
        }



i m doing this. i know wcf has power to do this but how it does i dont' know plz help me to achieve this. thanx in advance.

解决方案

you can check
http://blogs.msdn.com/b/dotnetinterop/archive/2008/11/04/rest-in-wcf-varying-response-content-type-based-on-http-request-headers.aspx


http://www.kailashtandel.com/2013/12/callling-wcf-rest-service-from-cross.html[^]


这篇关于wcf - 动态返回类型xml或json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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