如何从一个WCF REST方法返回一个自定义HTTP状态code? [英] How can I return a custom HTTP status code from a WCF REST method?
问题描述
如果出现错误的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$c$c>您可以访问,它有一个<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.weboperationcontext.outgoingresponse.aspx"><$c$c>OutgoingResponse$c$c>类型的财产<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.outgoingwebresponsecontext.aspx"><$c$c>OutgoingWebResponseContext$c$c>其中有一个<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.outgoingwebresponsecontext.status$c$c.aspx"><$c$c>Status$c$c$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屋!