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

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

问题描述

如果出现错误的WCF REST调用,如所请求的资源是找不到的,我怎么能与HTTP响应code打(它设定成类似HTTP 404为例),在我的OperationContract的方法?

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?

推荐答案

有一个<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.weboperationcontext.aspx"><$c$c>WebOperationContext您可以访问,它有一个<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.weboperationcontext.outgoingresponse.aspx"><$c$c>OutgoingResponse类型的财产<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.outgoingwebresponsecontext.aspx"><$c$c>OutgoingWebResponseContext其中有一个<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.outgoingwebresponsecontext.status$c$c.aspx"><$c$c>Status$c$c属性可设置

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状态code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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