UtilJsonApiSerializer.Serialization.Documents.CompoundDocument.GetPrimaryResourceHref C# (CSharp) Méthode

GetPrimaryResourceHref() public méthode

public GetPrimaryResourceHref ( ) : string
Résultat string
        public string GetPrimaryResourceHref()
        {
            var resource = Data as Dictionary<string, object>;
            if (resource != null && resource.ContainsKey("href"))
            {
                return resource["href"].ToString();
            }

            var resourceList = Data as List<Dictionary<string, object>>;
            var href = resourceList.FirstOrDefault().FirstOrDefault(kvp => kvp.Key == "href").Value as string;

            return href ?? string.Empty;
        }

Usage Example

        private static void HandlePostRequests(HttpActionExecutedContext actionExecutedContext, CompoundDocument transformed)
        {
            if (actionExecutedContext.Request.Method != HttpMethod.Post)
            {
                return;
            }

            var primaryResourceHref = transformed.GetPrimaryResourceHref();
            if (String.IsNullOrEmpty(primaryResourceHref))
            {
                return;
            }

            actionExecutedContext.Response.Headers.Location = new Uri(primaryResourceHref);
            actionExecutedContext.Response.StatusCode = HttpStatusCode.Created;
        }