aspnetcore/samples/RoutingSample/PrefixRoute.cs

59 lines
1.6 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;
using System.Threading.Tasks;
using Microsoft.AspNet.Routing;
namespace RoutingSample
{
internal class PrefixRoute : IRouter
{
private readonly IRouter _target;
private readonly string _prefix;
public PrefixRoute(IRouter target, string prefix)
{
_target = target;
if (prefix == null)
{
prefix = "/";
}
else if (prefix.Length > 0 && prefix[0] != '/')
{
// owin.RequestPath starts with a /
prefix = "/" + prefix;
}
if (prefix.Length > 1 && prefix[prefix.Length - 1] == '/')
{
prefix = prefix.Substring(0, prefix.Length - 1);
}
_prefix = prefix;
}
public async Task RouteAsync(RouteContext context)
{
if (context.RequestPath.StartsWith(_prefix, StringComparison.OrdinalIgnoreCase))
{
if (context.RequestPath.Length > _prefix.Length)
{
var lastCharacter = context.RequestPath[_prefix.Length];
if (lastCharacter != '/' && lastCharacter != '#' && lastCharacter != '?')
{
return;
}
}
await _target.RouteAsync(context);
}
}
public string GetVirtualPath(VirtualPathContext context)
{
return null;
}
}
}