Ejemplo n.º 1
0
        /// <summary>Convert an OData response to a Nancy response</summary>
        private Response GetResponse(ODataResponse oResp)
        {
            var resp = new Response().WithStatusCode(HttpStatusCode.OK);

            foreach (var header in oResp.Headers)
            {
                resp.WithHeader(header.Key, header.Value);
            }
            resp.Contents = oResp.WriteBytes;
            return(resp);
        }
Ejemplo n.º 2
0
        /// <summary>Create an OData response based on the URL</summary>
        private Response GetResponse(ODataVersion version)
        {
            // Parse the URL and specify the number of segments at the root of the
            // path
            var uri = OData.Parse(Request.Url.ToString(), version)
                      .WithRootSegmentCount(2);

            // Return response for the root of the path
            var path = uri.PathBuilder().ToString();

            if (string.IsNullOrEmpty(path))
            {
                return(GetResponse(ODataResponse.FromModel_Service(uri, _model)));
            }
            if (path.StartsWith("$metadata", StringComparison.OrdinalIgnoreCase))
            {
                return(GetResponse(ODataResponse.FromModel_Edmx(uri, _model)));
            }
            if (path.StartsWith("$swagger", StringComparison.OrdinalIgnoreCase))
            {
                return(GetResponse(ODataResponse.FromModel_Swagger(uri, _model, new Version(1, 0))));
            }

            // Query the data source and return the appropriate response
            ODataResponse oResp;

            try
            {
                oResp = uri
                        .Execute(_products, new ExecutionSettings().WithEdmModel(_model))
                        .CreateResponse(_model, null);
            }
            catch (Exception ex)
            {
                oResp = ODataResponse.FromException(uri, ex, false);
            }
            return(GetResponse(oResp));
        }