Skip to content

Commit cfb6dee

Browse files
authored
Merge pull request #173 from WebApiContrib/feature/actionresults
Incorporate action result executors and a set of helper methods around that
2 parents efc0fab + 62a1b9f commit cfb6dee

File tree

8 files changed

+1015
-5
lines changed

8 files changed

+1015
-5
lines changed

src/WebApiContrib.Core/Results/HttpContextExtensions.cs

Lines changed: 551 additions & 0 deletions
Large diffs are not rendered by default.

src/WebApiContrib.Core/WebApiContrib.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Community Contributions for ASP.NET Core. Add-ons and extensions to help improve your work with ASP.NET Core and ASP.NET Core MVC.</Description>
5-
<VersionPrefix>2.1.1</VersionPrefix>
5+
<VersionPrefix>2.2.0</VersionPrefix>
66
<Authors>filipw;WebApiContrib Contributors</Authors>
77
<TargetFrameworks>netstandard2.0</TargetFrameworks>
88
<AssemblyName>WebApiContrib.Core</AssemblyName>
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.AspNetCore.Http;
6+
using WebApiContrib.Core.Results;
7+
using Microsoft.AspNetCore.Routing;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Newtonsoft.Json;
10+
using System.Text;
11+
using System.IO;
12+
using Microsoft.Extensions.FileProviders;
13+
14+
namespace WebApiContrib.Core.Tests
15+
{
16+
public class Item
17+
{
18+
public string Name { get; set; }
19+
}
20+
21+
public class ActionResultsStartup
22+
{
23+
public IConfigurationRoot Configuration { get; }
24+
25+
public ActionResultsStartup(IHostingEnvironment env)
26+
{
27+
env.WebRootFileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
28+
29+
var builder = new ConfigurationBuilder()
30+
.SetBasePath(env.ContentRootPath)
31+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
32+
.AddEnvironmentVariables();
33+
Configuration = builder.Build();
34+
}
35+
36+
public void ConfigureServices(IServiceCollection services)
37+
{
38+
services.AddMvcCore().AddJsonFormatters();
39+
}
40+
41+
public void Configure(IApplicationBuilder app)
42+
{
43+
app.Map("/ok", b =>
44+
{
45+
b.Use(async (ctx, next) =>
46+
{
47+
await ctx.Ok();
48+
});
49+
});
50+
51+
app.Map("/ok-with-object", b =>
52+
{
53+
b.Use(async (ctx, next) =>
54+
{
55+
await ctx.Ok(new Item { Name = "test" });
56+
});
57+
});
58+
59+
app.Map("/accepted", b =>
60+
{
61+
b.Use(async (ctx, next) =>
62+
{
63+
await ctx.Accepted();
64+
});
65+
});
66+
67+
app.Map("/accepted-with-object", b =>
68+
{
69+
b.Use(async (ctx, next) =>
70+
{
71+
await ctx.Accepted(new Item { Name = "test" });
72+
});
73+
});
74+
75+
app.Map("/nocontent", b =>
76+
{
77+
b.Use(async (ctx, next) =>
78+
{
79+
await ctx.NoContent();
80+
});
81+
});
82+
83+
app.Map("/notfound", b =>
84+
{
85+
b.Use(async (ctx, next) =>
86+
{
87+
await ctx.NotFound();
88+
});
89+
});
90+
91+
app.Map("/notfound-with-object", b =>
92+
{
93+
b.Use(async (ctx, next) =>
94+
{
95+
await ctx.NotFound(new Item { Name = "test" });
96+
});
97+
});
98+
99+
app.Map("/conflict", b =>
100+
{
101+
b.Use(async (ctx, next) =>
102+
{
103+
await ctx.Conflict();
104+
});
105+
});
106+
107+
app.Map("/conflict-with-object", b =>
108+
{
109+
b.Use(async (ctx, next) =>
110+
{
111+
await ctx.Conflict(new Item { Name = "test" });
112+
});
113+
});
114+
115+
app.Map("/created", b =>
116+
{
117+
b.Use(async (ctx, next) =>
118+
{
119+
await ctx.Created("https://foo.bar", new Item { Name = "test" });
120+
});
121+
});
122+
123+
app.Map("/badrequest", b =>
124+
{
125+
b.Use(async (ctx, next) =>
126+
{
127+
await ctx.BadRequest();
128+
});
129+
});
130+
131+
app.Map("/badrequest-with-object", b =>
132+
{
133+
b.Use(async (ctx, next) =>
134+
{
135+
await ctx.BadRequest(new Item { Name = "test" });
136+
});
137+
});
138+
139+
app.Map("/unauthorized", b =>
140+
{
141+
b.Use(async (ctx, next) =>
142+
{
143+
await ctx.Unauthorized();
144+
});
145+
});
146+
147+
app.Map("/unauthorized-with-object", b =>
148+
{
149+
b.Use(async (ctx, next) =>
150+
{
151+
await ctx.Unauthorized(new Item { Name = "test" });
152+
});
153+
});
154+
155+
app.Map("/forbidden", b =>
156+
{
157+
b.Use(async (ctx, next) =>
158+
{
159+
await ctx.Forbid();
160+
});
161+
});
162+
163+
app.Map("/teapot", b =>
164+
{
165+
b.Use(async (ctx, next) =>
166+
{
167+
await ctx.StatusCode(StatusCodes.Status418ImATeapot);
168+
});
169+
});
170+
171+
app.Map("/unprocessable", b =>
172+
{
173+
b.Use(async (ctx, next) =>
174+
{
175+
await ctx.UnprocessableEntity(new Item { Name = "test" });
176+
});
177+
});
178+
179+
app.Map("/content", b =>
180+
{
181+
b.Use(async (ctx, next) =>
182+
{
183+
await ctx.Content(JsonConvert.SerializeObject(new Item { Name = "test" }), "application/json");
184+
});
185+
});
186+
187+
app.Map("/redirect", b =>
188+
{
189+
b.Use(async (ctx, next) =>
190+
{
191+
await ctx.Redirect("https://foo.bar");
192+
});
193+
});
194+
195+
app.Map("/redirect-permanent", b =>
196+
{
197+
b.Use(async (ctx, next) =>
198+
{
199+
await ctx.RedirectPermanent("https://foo.bar");
200+
});
201+
});
202+
203+
app.Map("/redirect-preserve-temporary", b =>
204+
{
205+
b.Use(async (ctx, next) =>
206+
{
207+
await ctx.RedirectPreserveMethod("https://foo.bar");
208+
});
209+
});
210+
211+
app.Map("/redirect-preserve-permanent", b =>
212+
{
213+
b.Use(async (ctx, next) =>
214+
{
215+
await ctx.RedirectPermanentPreserveMethod("https://foo.bar");
216+
});
217+
});
218+
219+
app.Map("/local-redirect", b =>
220+
{
221+
b.Use(async (ctx, next) =>
222+
{
223+
await ctx.LocalRedirect("/foo");
224+
});
225+
});
226+
227+
app.Map("/local-redirect-permanent", b =>
228+
{
229+
b.Use(async (ctx, next) =>
230+
{
231+
await ctx.LocalRedirectPermanent("/foo");
232+
});
233+
});
234+
235+
app.Map("/local-redirect-preserve-temporary", b =>
236+
{
237+
b.Use(async (ctx, next) =>
238+
{
239+
await ctx.LocalRedirectPreserveMethod("/foo");
240+
});
241+
});
242+
243+
app.Map("/local-redirect-preserve-permanent", b =>
244+
{
245+
b.Use(async (ctx, next) =>
246+
{
247+
await ctx.LocalRedirectPermanentPreserveMethod("/foo");
248+
});
249+
});
250+
251+
app.Map("/problemdetails", b =>
252+
{
253+
b.Use(async (ctx, next) =>
254+
{
255+
await ctx.ValidationProblem(new ValidationProblemDetails
256+
{
257+
Title = "problem details",
258+
Instance = "error",
259+
Detail = "stack trace"
260+
});
261+
});
262+
});
263+
264+
app.Map("/file-byte", b =>
265+
{
266+
b.Use(async (ctx, next) =>
267+
{
268+
var file = Encoding.UTF8.GetBytes("file");
269+
await ctx.File(file, "text/plain", "file.txt");
270+
});
271+
});
272+
273+
app.Map("/file-stream", b =>
274+
{
275+
b.Use(async (ctx, next) =>
276+
{
277+
var file = new MemoryStream(Encoding.UTF8.GetBytes("file"));
278+
await ctx.File(file, "text/plain", "file.txt");
279+
});
280+
});
281+
282+
app.Map("/file-physical", b =>
283+
{
284+
b.Use(async (ctx, next) =>
285+
{
286+
var path = Path.GetFullPath(Path.Combine("test-files", "file.txt"));
287+
await ctx.PhysicalFile(path, "text/plain", "file.txt");
288+
});
289+
});
290+
291+
app.Map("/file-virtual", b =>
292+
{
293+
b.Use(async (ctx, next) =>
294+
{
295+
var path = Path.Combine("test-files", "file.txt");
296+
await ctx.File(path, "text/plain", "file.txt");
297+
});
298+
});
299+
}
300+
}
301+
}

0 commit comments

Comments
 (0)