Thursday, May 19, 2011

ODATA Custom Exceptions

 
http://msdn.microsoft.com/en-us/library/ee391967.aspx#Y1700

public class AuthenticationInterceptor : RequestInterceptor
{     public AuthenticationInterceptor() : base(false) { }     public override void ProcessRequest(ref RequestContext requestContext)     {         if (!IsValidApiKey(requestContext))             GenerateErrorResponse(requestContext,                 HttpStatusCode.Unauthorized,                 "Missing or invalid user key (supply via the Authorization header)");     }     public bool IsValidUserKey(Message req, string key, string uri)     {         ... // ommitted for brevity     }     public void GenerateErrorResponse(RequestContext requestContext,         HttpStatusCode statusCode, string errorMessage)     {         // The error message is padded so that IE shows the response by default         string errorHtml =             "<html><HEAD><TITLE>Request Error</TITLE></HEAD><BODY>" +             "<H1>Error processing request</H1><P>{0}</P></BODY></html>";         XElement response = XElement.Load(new StringReader(             string.Format(errorHtml, errorMessage)));         Message reply = Message.CreateMessage(MessageVersion.None, null, response);         HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty()         {             StatusCode = statusCode         };         responseProp.Headers[HttpResponseHeader.ContentType] = "text/html";         reply.Properties[HttpResponseMessageProperty.Name] = responseProp;         requestContext.Reply(reply);         // set the request context to null to terminate processing of this request         requestContext = null;     }
}

No comments:

Post a Comment