C# 如何从 WCF REST 方法返回自定义 HTTP 状态代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/140104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I return a custom HTTP status code from a WCF REST method?
提问by kgriffs
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?
如果 WCF REST 调用出现问题,例如未找到请求的资源,我如何在我的 OperationContract 方法中使用 HTTP 响应代码(例如将其设置为 HTTP 404 之类的代码)?
采纳答案by Eric Schoonover
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
您可以访问,它有一个OutgoingResponse
type 属性,OutgoingWebResponseContext
它有一个StatusCode
可以设置的属性。
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
回答by JarrettV
For 404 there is a built in method on the WebOperationContext.Current.OutgoingResponsecalled SetStatusAsNotFound(string message)that will set the status code to 404 and a status description with one call.
对于 404,WebOperationContext.Current.OutgoingResponse上有一个名为SetStatusAsNotFound(string message)的内置方法,它将通过一次调用将状态代码设置为 404 和状态描述。
Note there is also, SetStatusAsCreated(Uri location)that will set the status code to 201 and location header with one call.
请注意,还有SetStatusAsCreated(Uri location)将通过一次调用将状态代码设置为 201 和位置标头。
回答by Graeme Bradbury
If you need to return a reason body then have a look at WebFaultException
如果您需要返回原因正文,请查看WebFaultException
For example
例如
throw new WebFaultException<string>("Bar wasn't Foo'd", HttpStatusCode.BadRequest );
回答by Hydtechie
If you wish to see the status description in the header, REST method should make sure to return null from the Catch() section as below:
如果您希望在标题中查看状态描述,REST 方法应确保从 Catch() 部分返回 null,如下所示:
catch (ArgumentException ex)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
WebOperationContext.Current.OutgoingResponse.StatusDescription = ex.Message;
return null;
}
回答by OnlyMahesh
This did not work for me for WCF Data Services. Instead, you can use DataServiceException in case of Data Services. Found the following post useful. http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/f0cbab98-fcd7-4248-af81-5f74b019d8de
对于 WCF 数据服务,这对我不起作用。相反,您可以在数据服务的情况下使用 DataServiceException。发现以下帖子很有用。 http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/f0cbab98-fcd7-4248-af81-5f74b019d8de
回答by user5234326
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
throw new WebException("令牌码不正确", new InvalidTokenException());
参考:https: //social.msdn.microsoft.com/Forums/en-US/f6671de3-34ce-4b70-9a77-39ecf5d1b9c3/weboperationcontext-http-statuses-and-exceptions?forum =wcf
回答by eitzo
You can also return a statuscode and reason body with WebOperationContext's StatusCodeand StatusDescription:
您还可以使用WebOperationContext的StatusCode和StatusDescription返回状态代码和原因正文:
WebOperationContext context = WebOperationContext.Current;
context.OutgoingResponse.StatusCode = HttpStatusCode.OK;
context.OutgoingResponse.StatusDescription = "Your Message";