2

WCF で WEB API レスト サービスを作成または作成し始めました。比較的順調に進んでいます。ただし、小さな問題が発生しました。これを実装しました。

http://blogs.msdn.com/b/rjacobs/archive/2010/06/14/how-to-do-api-key-verification-for-rest-services-in-net-4.aspx

キー検証用。(残りのサービスの実装に似ているため、これが WCF WEB API の正しいアプローチであるかどうかはわかりません)。

とにかく、それはうまくいくようです。ただし、API キーが提供されていない場合、例外はブラウザーに表示されません。つまり、キーを提供すると正しく返されますが、提供しないと空白のページが表示されます。

  private static void CreateErrorReply(OperationContext operationContext, string key)
    {
        // The error message is padded so that IE shows the response by default
        using (var sr = new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + APIErrorHTML))
        {
            XElement response = XElement.Load(sr);
            using (Message reply = Message.CreateMessage(MessageVersion.None, null, response))
            {

                HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty() { StatusCode = HttpStatusCode.Unauthorized, StatusDescription = String.Format("'{0}' is an invalid API key", key) };
                responseProp.Headers[HttpResponseHeader.ContentType] = "text/html";
                reply.Properties[HttpResponseMessageProperty.Name] = responseProp;
                operationContext.RequestContext.Reply(reply);
                // set the request context to null to terminate processing of this request
                operationContext.RequestContext = null;

            }
        }
    }

これがエラーを示す代わりに、結果は空白の応答になります。誰でも助けることができますか?

4

0 に答える 0