1

POSTWCF を使用して REST サービスを開発していますが、無効な場合に返さなければならない HTTP ステータス コードの種類がわかりませんMessage。注: ここでのメッセージは、チャット メッセージ (テキストといくつかのデータ) のようなものです。

これは、WCF サービスを実装した方法です。

IServiceContract :

[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "/messages",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare)]
Message AddMessage(Message message);

サービスの実装:

public Message AddMessage(Message message)
{
    OutgoingWebResponseContext ctx =
        WebOperationContext.Current.OutgoingResponse;

    if (message == null)
    {
        ctx.StatusCode = System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
        ctx.StatusDescription = "message parameter is null";

        throw new ArgumentNullException("message", "AddMessage: message parameter is null");
    }

    using (var context = new AdnLineContext())
    {
        context.Entry(message).State = EntityState.Added;
        context.SaveChanges();
    }

    return message;
}

今私はRequestedRangeNotSatisfiable(HTTP 416)を使用しています。しかし、これが無効なを POST したときに返される HTTP ステータス コードであるかどうかはわかりませんMessage

無効なオブジェクトを POST する場合、どのような HTTP ステータス コードを返す必要がありますか?

4

2 に答える 2

3

通常、例外を管理できる場合は、4xx HTTP ステータス コードを使用します。そうしないと、5xx HTTP ステータス コードが生成されます。

あなたの例では、を使用できます400 Bad Request HTTP Status code

10.4.1 400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

W3Cから

于 2013-08-07T09:19:09.583 に答える