This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
CreatedResult.cs
58 lines (52 loc) · 1.76 KB
/
CreatedResult.cs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Actions;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// An <see cref="ActionResult"/> that returns a Created (201) response with a Location header.
/// </summary>
public class CreatedResult : ObjectResult
{
private string _location;
/// <summary>
/// Initializes a new instance of the <see cref="CreatedResult"/> class with the values
/// provided.
/// </summary>
/// <param name="location">The location at which the content has been created.</param>
/// <param name="value">The value to format in the entity body.</param>
public CreatedResult([NotNull] string location, object value)
: base(value)
{
Location = location;
StatusCode = StatusCodes.Status201Created;
}
/// <summary>
/// Gets or sets the location at which the content has been created.
/// </summary>
public string Location
{
get
{
return _location;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_location = value;
}
}
/// <inheritdoc />
protected override void OnFormatting([NotNull] ActionContext context)
{
context.HttpContext.Response.Headers[HeaderNames.Location] = Location;
}
}
}