devicehive-.net /src/Server/DeviceHive.API/Filters/HttpCreatedResponseAttribute.cs

Language C# Lines 31
MD5 Hash bfa2fde6b7bcc29f6c51a6ad72ef68f8
Repository https://github.com/oryol/devicehive-.net.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http.Filters;
using System.Web.Http.Routing;
using Newtonsoft.Json.Linq;

namespace DeviceHive.API.Filters
{
    public class HttpCreatedResponseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            if (context.Response != null && context.Response.IsSuccessStatusCode)
            {
                context.Response.StatusCode = HttpStatusCode.Created;
                context.Response.Content.ReadAsStringAsync().ContinueWith(task =>
                    {
                        var result = JObject.Parse(task.Result);
                        if (result["id"] != null)
                        {
                            var controller = context.ActionContext.ControllerContext.ControllerDescriptor.ControllerName;
                            var route = new UrlHelper(context.Request).Route(null, new { controller = controller, id = result["id"] });
                            context.Response.Headers.Location = new Uri(context.Request.RequestUri, route);
                        }
                    }).Wait();
            }
        }
    }
}
Back to Top