Remove dependency on routing in sample

Removing the routing dependency since this is moving lower in the stack.
This commit is contained in:
Ryan Nowak 2016-02-02 09:03:53 -08:00
parent 492c0798b1
commit 96063e2476
2 changed files with 17 additions and 23 deletions

View File

@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -18,8 +17,6 @@ namespace AntiforgerySample
{ {
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddRouting();
// Angular's default header name for sending the XSRF token. // Angular's default header name for sending the XSRF token.
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN"); services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
@ -45,30 +42,28 @@ namespace AntiforgerySample
app.UseDefaultFiles(); app.UseDefaultFiles();
app.UseStaticFiles(); app.UseStaticFiles();
var routes = new RouteBuilder(app); app.Map("/api/items", a => a.Run(async context =>
routes.MapGet("api/items", (HttpContext context) =>
{ {
var items = repository.GetItems(); if (string.Equals("GET", context.Request.Method, StringComparison.OrdinalIgnoreCase))
return context.Response.WriteAsync(JsonConvert.SerializeObject(items));
});
routes.MapPost("api/items", async (HttpContext context) =>
{
// This will throw if the token is invalid.
await antiforgery.ValidateRequestAsync(context);
var serializer = new JsonSerializer();
using (var reader = new JsonTextReader(new StreamReader(context.Request.Body)))
{ {
var item = serializer.Deserialize<TodoItem>(reader); var items = repository.GetItems();
repository.Add(item); await context.Response.WriteAsync(JsonConvert.SerializeObject(items));
} }
else if (string.Equals("POST", context.Request.Method, StringComparison.OrdinalIgnoreCase))
{
// This will throw if the token is invalid.
await antiforgery.ValidateRequestAsync(context);
context.Response.StatusCode = 204; var serializer = new JsonSerializer();
}); using (var reader = new JsonTextReader(new StreamReader(context.Request.Body)))
{
var item = serializer.Deserialize<TodoItem>(reader);
repository.Add(item);
}
app.UseRouter(routes.Build()); context.Response.StatusCode = 204;
}
}));
} }
public static void Main(string[] args) public static void Main(string[] args)

View File

@ -6,7 +6,6 @@
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0-*", "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-*",
"Microsoft.AspNetCore.Http.Extensions": "1.0.0-*", "Microsoft.AspNetCore.Http.Extensions": "1.0.0-*",
"Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*",
"Microsoft.AspNetCore.Routing.Extensions": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*", "Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*", "Microsoft.NETCore.Platforms": "1.0.1-*",