经过一整天的努力,我发现这是由IIS设置引起的。
在IIS中的API项目下,在“身份验证”菜单下,将“表单身份验证”设置为“启用”。我关闭了此“功能”,上面的代码按预期开始工作。我发现这是由于团队中的另一个开发人员将代码放入web.config文件中,从而更改了IIS中的设置。特别:
<?xml version="1.0" encoding="utf-8"?><configuration> ... <system.web> <authentication mode="Forms" /> </system.web> ...</configuration>
此外,通过使用WebOperationContext OutgoingResponse对象上的ContentType属性,我能够使Content-
Type标头正确显示。
// Get the outgoing response portion of the current contextvar response = WebOperationContext.Current.OutgoingResponse;// Add ContentType header that specifies we are using JSONresponse.ContentType = new MediaTypeHeaderValue("application/json").ToString();


