Port IRouteConstraint to the dispatcher project

This commit is contained in:
Ryan Nowak 2017-10-20 14:04:22 -07:00
parent fdc5f21428
commit df78db934d
3 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Dispatcher
{
/// <summary>
/// Represents the purpose for invoking an <see cref="IDispatcherValueConstraint"/>.
/// </summary>
public enum ConstraintPurpose
{
/// <summary>
/// A request URL is being processed by the dispatcher.
/// </summary>
IncomingRequest,
/// <summary>
/// A URL is being created by a template.
/// </summary>
TemplateExecution,
}
}

View File

@ -0,0 +1,109 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Dispatcher
{
/// <summary>
/// A context object for <see cref="IDispatcherValueConstraint"/>.
/// </summary>
public class DispatcherValueConstraintContext
{
private HttpContext _httpContext;
private DispatcherValueCollection _values;
private ConstraintPurpose _purpose;
private string _key;
/// <summary>
/// Creates a new <see cref="DispatcherValueConstraintContext"/>.
/// </summary>
public DispatcherValueConstraintContext()
{
}
/// <summary>
/// Creates a new <see cref="DispatcherValueConstraintContext"/> for the current request.
/// </summary>
/// <param name="httpContext">The <see cref="Http.HttpContext"/> associated with the current request.</param>
/// <param name="values">The <see cref="DispatcherValueCollection"/> for the current operation.</param>
/// <param name="purpose">The purpose for invoking the constraint.</param>
public DispatcherValueConstraintContext(HttpContext httpContext, DispatcherValueCollection values, ConstraintPurpose purpose)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
HttpContext = httpContext;
Values = values;
Purpose = purpose;
}
/// <summary>
/// Gets or sets the <see cref="Http.HttpContext"/> associated with the current request.
/// </summary>
public HttpContext HttpContext
{
get => _httpContext;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_httpContext = value;
}
}
/// <summary>
/// Gets or sets the key associated with the current <see cref="IDispatcherValueConstraint"/>.
/// </summary>
public string Key
{
get => _key;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_key = value;
}
}
/// <summary>
/// Gets or sets the purpose of executing the <see cref="IDispatcherValueConstraint"/>.
/// </summary>
public ConstraintPurpose Purpose
{
get => _purpose;
set => _purpose = value;
}
/// <summary>
/// Gets or sets the <see cref="DispatcherValueCollection"/> associated with the current operation.
/// </summary>
public DispatcherValueCollection Values
{
get => _values;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_values = value;
}
}
}
}

View File

@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Dispatcher
{
/// <summary>
/// Defines the contract that a class must implement in order to check whether a URL parameter
/// value is valid for a constraint.
/// </summary>
public interface IDispatcherValueConstraint
{
/// <summary>
/// Determines whether the current operation should succeed or fail, typically by validating one or
/// more values in <see cref="DispatcherValueConstraintContext.Values"/>.
/// </summary>
/// <param name="context">The <see cref="DispatcherValueConstraintContext"/> associated with the current operation.</param>
/// <returns><c>true</c> if the current operation should proceed; otherwise <c>false</c>.</returns>
bool Match(DispatcherValueConstraintContext context);
}
}