【发布时间】:2010-05-25 10:01:15
【问题描述】:
我想创建一个全局选项,当 REST 调用包含 &format=json 时,将响应输出为 JSON 字符串。
如果我在我的方法中输入以下字符串,它会起作用:
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
但是,如果我在 Global.asax 文件中的任何位置添加这一行,我会得到当前上下文的 nullException:
String format = "";
if (HttpContext.Current.Request.QueryString["format"] != null)
format = HttpContext.Current.Request.QueryString["format"];
if (String.Equals("json", format, StringComparison.OrdinalIgnoreCase))
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.Format = System.ServiceModel.Web.WebMessageFormat.Json;
这里触发了异常:
System.ServiceModel.Web.WebOperationContext.Current
有人知道我如何在全局范围内添加此功能 (WCF)?
【问题讨论】:
-
我不一定需要将此添加到 Global.asax。我只需要它是全球性的,这样它将影响所有 450 个操作。