aspnetcore/samples/RoutingSample/Startup.cs

36 lines
1.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Routing.Owin;
using Owin;
namespace RoutingSample
{
internal class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var routes = appBuilder.UseRouter();
OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1"));
OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!"));
routes.Add(new PrefixRoute(endpoint1, "api/store"));
routes.Add(new PrefixRoute(endpoint1, "api/checkout"));
routes.Add(new PrefixRoute(endpoint2, "hello/world"));
routes.Add(new PrefixRoute(endpoint1, ""));
}
private static async Task WriteToBodyAsync(IDictionary<string, object> context, string text)
{
var stream = (Stream)context["owin.ResponseBody"];
byte[] bytes = Encoding.UTF8.GetBytes(text);
await stream.WriteAsync(bytes, 0, bytes.Length);
}
}
}