Causing HTTP error status to be returned in WCF

后端 未结 3 723
一向
一向 2021-02-10 02:28

How can I get my WCF service to communicate errors in a RESTful manner? Specifically, if the caller passes invalid query string parameters to my method, I\'d like to have a 400

3条回答
  •  半阙折子戏
    2021-02-10 02:58

    I wanted to implement the same solution you are asking, the link below worked perfect when you want to play with HTTP status codes.

    How can I return a custom HTTP status code from a WCF REST method?

    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;
    

提交回复
热议问题