forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseFilterAttribute.cs
More file actions
55 lines (48 loc) · 1.79 KB
/
ResponseFilterAttribute.cs
File metadata and controls
55 lines (48 loc) · 1.79 KB
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
using System;
using ServiceStack.Server;
using ServiceStack.ServiceHost;
using ServiceStack.Common;
namespace ServiceStack.ServiceInterface
{
/// <summary>
/// Base class to create response filter attributes only for specific HTTP methods (GET, POST...)
/// </summary>
public abstract class ResponseFilterAttribute : Attribute, IHasResponseFilter
{
public int Priority { get; set; }
public ApplyTo ApplyTo { get; set; }
public ResponseFilterAttribute()
{
ApplyTo = ApplyTo.All;
}
/// <summary>
/// Creates a new <see cref="ResponseFilterAttribute"/>
/// </summary>
/// <param name="applyTo">Defines when the filter should be executed</param>
public ResponseFilterAttribute(ApplyTo applyTo)
{
ApplyTo = applyTo;
}
public void ResponseFilter(IHttpRequest req, IHttpResponse res, object response)
{
ApplyTo httpMethod = req.HttpMethodAsApplyTo();
if (ApplyTo.Has(httpMethod))
this.Execute(req, res, response);
}
/// <summary>
/// This method is only executed if the HTTP method matches the <see cref="ApplyTo"/> property.
/// </summary>
/// <param name="req">The http request wrapper</param>
/// <param name="res">The http response wrapper</param>
/// <param name="requestDto">The response DTO</param>
public abstract void Execute(IHttpRequest req, IHttpResponse res, object responseDto);
/// <summary>
/// Create a ShallowCopy of this instance.
/// </summary>
/// <returns></returns>
public virtual IHasResponseFilter Copy()
{
return (IHasResponseFilter)this.MemberwiseClone();
}
}
}