Create a new shared package, Microsoft.AspNet.SpaServices, to hold MapSpaFallbackRoute (and other common infrastructure yet to be added)

This commit is contained in:
SteveSandersonMS
2015-12-09 15:13:56 +00:00
parent 4a5b9e62ef
commit d4f04d211f
6 changed files with 110 additions and 60 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.SpaServices
{
internal class SpaRouteConstraint : IRouteConstraint
{
private readonly string clientRouteTokenName;
public SpaRouteConstraint(string clientRouteTokenName) {
if (string.IsNullOrEmpty(clientRouteTokenName)) {
throw new ArgumentException("Value cannot be null or empty", "clientRouteTokenName");
}
this.clientRouteTokenName = clientRouteTokenName;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary<string, object> values, RouteDirection routeDirection)
{
var clientRouteValue = (values[this.clientRouteTokenName] as string) ?? string.Empty;
return !HasDotInLastSegment(clientRouteValue);
}
private bool HasDotInLastSegment(string uri)
{
var lastSegmentStartPos = uri.LastIndexOf('/');
return uri.IndexOf('.', lastSegmentStartPos + 1) >= 0;
}
}
}