如何从 WCF REST 方法返回自定义 HTTP 状态代码? [英] How can I return a custom HTTP status code from a WCF REST method?

查看:39
本文介绍了如何从 WCF REST 方法返回自定义 HTTP 状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 WCF REST 调用出现问题,例如未找到请求的资源,我该如何在我的 OperationContract 方法中使用 HTTP 响应代码(例如将其设置为 HTTP 404 之类的代码)?

If something goes wrong in a WCF REST call, such as the requested resource is not found, how can I play with the HTTP response code (setting it to something like HTTP 404, for example) in my OperationContract method?

推荐答案

有一个 WebOperationContext,您可以访问它,并且它有一个 OutgoingResponse 类型的属性 OutgoingWebResponseContext 有一个 StatusCode 可以设置的属性.

There is a WebOperationContext that you can access and it has a OutgoingResponse property of type OutgoingWebResponseContext which has a StatusCode property that can be set.

WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

这篇关于如何从 WCF REST 方法返回自定义 HTTP 状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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