forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoController.cs
More file actions
118 lines (99 loc) · 2.98 KB
/
Copy pathDemoController.cs
File metadata and controls
118 lines (99 loc) · 2.98 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Linq;
using System.Threading.Tasks;
using uhttpsharp;
using uhttpsharp.Attributes;
using uhttpsharp.Controllers;
using uhttpsharp.Handlers;
namespace uhttpsharpdemo.Controllers
{
public class EmptyPipeline : IPipeline
{
public Task<IControllerResponse> Go(Func<Task<IControllerResponse>> injectedTask, IHttpContext context)
{
return injectedTask();
}
}
public class JsonController : IController
{
public class Question
{
public string TheQuestion { get; set; }
}
public JsonController(int id)
{
}
[HttpMethod(HttpMethods.Post)]
public IControllerResponse Post([FromBody] Question question)
{
return Response.Render(HttpResponseCode.Ok, question).Result;
}
public IPipeline Pipeline => new EmptyPipeline();
}
public class MyController
{
private readonly int _id;
public MyController(int id)
{
_id = id;
}
public MyController()
{
}
[HttpMethod(HttpMethods.Post)]
public Task<IControllerResponse> Post([FromPost("a")] MyRequest request, [FromHeaders("header")] string hello,
[FromQuery("query")] string world)
{
return Response.Render(HttpResponseCode.Ok, null);
}
[Indexer]
public Task<object> Get(IHttpContext context, int id)
{
return Task.FromResult<object>(new MyController(id));
}
}
public class MyRequest : IValidate
{
public int A { get; set; }
public void Validate(IErrorContainer container)
{
if (A == 0)
{
container.Log("A cannot be zero");
}
}
}
internal class BaseController : IController
{
[HttpMethod(HttpMethods.Get)]
public Task<IControllerResponse> Get()
{
return Response.Render(HttpResponseCode.Ok, new { Hello = "Base!", Kaki = Enumerable.Range(0, 10000) });
}
[HttpMethod(HttpMethods.Post)]
public Task<IControllerResponse> Post([FromBody] MyRequest a)
{
return Response.Render(HttpResponseCode.Ok, a);
}
public virtual IPipeline Pipeline => new EmptyPipeline();
public IController Derived => new DerivedController();
}
internal class DerivedController : BaseController
{
[HttpMethod(HttpMethods.Get)]
public new Task<IControllerResponse> Get()
{
return Response.Render(HttpResponseCode.Ok, new { Hello = "Derived!" });
}
[Indexer(0)]
public Task<IController> Indexer(IHttpContext context, int hey)
{
return Task.FromResult<IController>(this);
}
[Indexer(1)]
public Task<IController> Indexer(IHttpContext context, string hey)
{
return Task.FromResult<IController>(this);
}
}
}