如何使用查询字符串创建动态响应?WCF动态响应格式
我想根据用户在邮件正文内部的具体内容动态输出响应格式。
例如,如果用户输入“json”,“xml”,“soap”,它将返回相应的格式。提前致谢。
public interface IReg
{
[OperationContract]
[WebInvoke]
MemberBasic Login(string uniqueID, string password, string returnFormat);
}
[DataContract(Namespace = "", IsReference=false)]
[Serializable()]
public class MemberBasic
{
#region Properties
[DataMember]
public DateTime LastModified
{
get;
set;
}
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public sealed class RWS : IReg
{
public MemberBasic Login(string uniqueID, string password, string returnFormat)
{
MemberBasic result = new MemberBasic();
setReturnFormat(returnFormat);
return result;
}
}
private static void Init(string returnFormat)
{
var response = WebOperationContext.Current.OutgoingResponse;
response.Headers.Add("cache-Control", "no-cache");
response.Headers.Add("Last-Modified", string.Format("{0:r}", DateTime.Today));
switch (returnFormat)
{
case "xml":
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
WebOperationContext.Current.OutgoingRequest.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json");
} break;
case "json":
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
} break;
default:
{
throw new ArgumentException("Return Format unrecognized; cannot complete request.",
"returnFormat");
}
}
}
您能向我们解释您的示例代码吗?它工作吗?如果不是,它是如何失败的? –
它失败!我想知道用户如何传递他们想要的返回类型,并且只显示它。 – James
它只会返回肥皂请求。 – James