public MainModule() { Get["/"] = _ => { throw new ImgAzyobuziException(Errors.SelectAPI); }; Get["/regex.json"] = _ => Response.AsJson(context.GetRegex()); Get["/sizes.json"] = _ => { var model = context.GetSizes((string)Request.Query.uri); context.WriteLog(model, Request.Headers.UserAgent, Request.Headers.Referrer); return Response.AsJson(model).WithExpires(); }; Get["/redirect.json"] = _ => { var model = context.Redirect((string)Request.Query.uri, (string)Request.Query.size); context.WriteLog(model, Request.Headers.UserAgent, Request.Headers.Referrer); return Response.AsRedirect(model.Location).WithExpires(); }; OnError += (ctx, ex) => { var model = new ErrorModel(ex); return Response.AsJson(model, (HttpStatusCode)Errors.ErrorTable[model.Code].StatusCode); }; }
public void Handle(HttpStatusCode statusCode, NancyContext context) { if (context.Response != null && context.Response.Contents != null && !ReferenceEquals(context.Response.Contents, Response.NoBody)) return; var model = new ErrorModel( statusCode == HttpStatusCode.NotFound ? Errors.ApiNotFound : statusCode == HttpStatusCode.MethodNotAllowed ? Errors.InvalidMethod : Errors.UnknownError ); context.Response = new JsonResponse(model, new JsonNetSerializer()).WithStatusCode(statusCode); }