Replacing NotNullAttribute with exceptions

This commit is contained in:
Pranav K 2015-09-10 22:16:45 -07:00
parent 7cd8db6695
commit c6941e797f
42 changed files with 772 additions and 218 deletions

View File

@ -1,15 +1,25 @@
// 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.AspNet.Routing;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
public static class BuilderExtensions
{
public static IApplicationBuilder UseRouter([NotNull] this IApplicationBuilder builder, [NotNull] IRouter router)
public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IRouter router)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (router == null)
{
throw new ArgumentNullException(nameof(router));
}
return builder.UseMiddleware<RouterMiddleware>(router);
}
}

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class BoolRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -1,9 +1,9 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -16,8 +16,13 @@ namespace Microsoft.AspNet.Routing
/// Initializes a new instance of the <see cref="CompositeRouteConstraint" /> class.
/// </summary>
/// <param name="constraints">The child constraints that must match for this constraint to match.</param>
public CompositeRouteConstraint([NotNull] IEnumerable<IRouteConstraint> constraints)
public CompositeRouteConstraint(IEnumerable<IRouteConstraint> constraints)
{
if (constraints == null)
{
throw new ArgumentNullException(nameof(constraints));
}
Constraints = constraints;
}
@ -27,12 +32,33 @@ namespace Microsoft.AspNet.Routing
public IEnumerable<IRouteConstraint> Constraints { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
foreach (var constraint in Constraints)
{
if (!constraint.Match(httpContext, route, routeKey, values, routeDirection))

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -21,12 +20,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class DateTimeRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class DecimalRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class DoubleRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class FloatRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -17,12 +16,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class GuidRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class IntRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -72,12 +71,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public int MaxLength { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -15,12 +14,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public class LongRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{
@ -31,7 +51,7 @@ namespace Microsoft.AspNet.Routing.Constraints
long result;
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return Int64.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
return long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
}
return false;

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -35,12 +34,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public int MaxLength { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -29,12 +28,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public long Max { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -35,12 +34,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public int MinLength { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -29,12 +28,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public long Min { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -1,9 +1,9 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -12,19 +12,45 @@ namespace Microsoft.AspNet.Routing.Constraints
/// </summary>
public class OptionalRouteConstraint : IRouteConstraint
{
public OptionalRouteConstraint([NotNull] IRouteConstraint innerConstraint)
public OptionalRouteConstraint(IRouteConstraint innerConstraint)
{
if (innerConstraint == null)
{
throw new ArgumentNullException(nameof(innerConstraint));
}
InnerConstraint = innerConstraint;
}
public IRouteConstraint InnerConstraint { get; }
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value))
{

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -43,12 +42,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public long Max { get; private set; }
/// <inheritdoc />
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -1,7 +1,7 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Routing.Constraints
/// Initializes a new instance of the <see cref="RegexInlineRouteConstraint" /> class.
/// </summary>
/// <param name="regexPattern">The regular expression pattern to match.</param>
public RegexInlineRouteConstraint([NotNull] string regexPattern)
public RegexInlineRouteConstraint(string regexPattern)
: base(regexPattern)
{
}

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -14,13 +13,23 @@ namespace Microsoft.AspNet.Routing.Constraints
{
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(10);
public RegexRouteConstraint([NotNull] Regex regex)
public RegexRouteConstraint(Regex regex)
{
if (regex == null)
{
throw new ArgumentNullException(nameof(regex));
}
Constraint = regex;
}
public RegexRouteConstraint([NotNull] string regexPattern)
public RegexRouteConstraint(string regexPattern)
{
if (regexPattern == null)
{
throw new ArgumentNullException(nameof(regexPattern));
}
Constraint = new Regex(
regexPattern,
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase,
@ -29,12 +38,33 @@ namespace Microsoft.AspNet.Routing.Constraints
public Regex Constraint { get; private set; }
public bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> routeValues,
RouteDirection routeDirection)
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> routeValues,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (routeValues == null)
{
throw new ArgumentNullException(nameof(routeValues));
}
object routeValue;
if (routeValues.TryGetValue(routeKey, out routeValue)

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Constraints
{
@ -20,12 +19,32 @@ namespace Microsoft.AspNet.Routing.Constraints
{
/// <inheritdoc />
public bool Match(
[NotNull]HttpContext httpContext,
[NotNull]IRouter route,
[NotNull]string routeKey,
[NotNull]IDictionary<string, object> values,
HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Routing
@ -39,8 +38,13 @@ namespace Microsoft.AspNet.Routing
/// The entire string "arg1, arg2, 12" will be treated as a single argument.
/// In all other cases arguments are split at comma.
/// </example>
public virtual IRouteConstraint ResolveConstraint([NotNull] string inlineConstraint)
public virtual IRouteConstraint ResolveConstraint(string inlineConstraint)
{
if (inlineConstraint == null)
{
throw new ArgumentNullException(nameof(inlineConstraint));
}
string constraintKey;
string argumentString;
var indexOfFirstOpenParens = inlineConstraint.IndexOf('(');

View File

@ -1,8 +1,6 @@
// 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 Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
/// <summary>
@ -15,6 +13,6 @@ namespace Microsoft.AspNet.Routing
/// </summary>
/// <param name="inlineConstraint">The inline constraint to resolve.</param>
/// <returns>The <see cref="IRouteConstraint"/> the inline constraint was resolved to.</returns>
IRouteConstraint ResolveConstraint([NotNull] string inlineConstraint);
IRouteConstraint ResolveConstraint(string inlineConstraint);
}
}

View File

@ -3,16 +3,15 @@
using System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
public interface IRouteConstraint
{
bool Match([NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] string routeKey,
[NotNull] IDictionary<string, object> values,
bool Match(HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection);
}
}

View File

@ -1,16 +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.
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
public static class InlineRouteParameterParser
{
public static TemplatePart ParseRouteParameter([NotNull] string routeParameter)
public static TemplatePart ParseRouteParameter(string routeParameter)
{
if (routeParameter == null)
{
throw new ArgumentNullException(nameof(routeParameter));
}
if (routeParameter.Length == 0)
{
return TemplatePart.CreateParameter(

View File

@ -1,15 +1,20 @@
// 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 Microsoft.Framework.Internal;
using System;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Routing.Logging.Internal
{
public static class LoggerExtensions
{
public static void WriteValues([NotNull] this ILogger logger, object values)
public static void WriteValues(this ILogger logger, object values)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
logger.Log(
logLevel: LogLevel.Verbose,
eventId: 0,

View File

@ -7,7 +7,6 @@ using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Routing
@ -31,8 +30,13 @@ namespace Microsoft.AspNet.Routing
get { return _routes.Count; }
}
public void Add([NotNull] IRouter router)
public void Add(IRouter router)
{
if (router == null)
{
throw new ArgumentNullException(nameof(router));
}
var namedRouter = router as INamedRouter;
if (namedRouter != null)
{

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -28,9 +27,19 @@ namespace Microsoft.AspNet.Routing
/// <param name="inlineConstraintResolver">The <see cref="IInlineConstraintResolver"/>.</param>
/// <param name="displayName">The display name (for use in error messages).</param>
public RouteConstraintBuilder(
[NotNull] IInlineConstraintResolver inlineConstraintResolver,
[NotNull] string displayName)
IInlineConstraintResolver inlineConstraintResolver,
string displayName)
{
if (inlineConstraintResolver == null)
{
throw new ArgumentNullException(nameof(inlineConstraintResolver));
}
if (displayName == null)
{
throw new ArgumentNullException(nameof(displayName));
}
_inlineConstraintResolver = inlineConstraintResolver;
_displayName = displayName;
@ -84,8 +93,18 @@ namespace Microsoft.AspNet.Routing
/// For example, the string <code>Product[0-9]+</code> will be converted to the regular expression
/// <code>^(Product[0-9]+)</code>. See <see cref="System.Text.RegularExpressions.Regex"/> for more details.
/// </remarks>
public void AddConstraint([NotNull] string key, [NotNull] object value)
public void AddConstraint(string key, object value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
var constraint = value as IRouteConstraint;
if (constraint == null)
{
@ -117,8 +136,18 @@ namespace Microsoft.AspNet.Routing
/// based on <paramref name="constraintText"/>. See <see cref="RouteOptions.ConstraintMap"/> to register
/// custom constraint types.
/// </remarks>
public void AddResolvedConstraint([NotNull] string key, [NotNull] string constraintText)
public void AddResolvedConstraint(string key, string constraintText)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (constraintText == null)
{
throw new ArgumentNullException(nameof(constraintText));
}
var constraint = _inlineConstraintResolver.ResolveConstraint(constraintText);
if (constraint == null)
{
@ -137,9 +166,14 @@ namespace Microsoft.AspNet.Routing
/// Sets the given key as optional.
/// </summary>
/// <param name="key">The key.</param>
public void SetOptional([NotNull] string key)
public void SetOptional(string key)
{
_optionalParameters.Add(key);
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
_optionalParameters.Add(key);
}
private void Add(string key, IRouteConstraint constraint)

View File

@ -1,9 +1,9 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Routing
@ -11,12 +11,32 @@ namespace Microsoft.AspNet.Routing
public static class RouteConstraintMatcher
{
public static bool Match(IReadOnlyDictionary<string, IRouteConstraint> constraints,
[NotNull] IDictionary<string, object> routeValues,
[NotNull] HttpContext httpContext,
[NotNull] IRouter route,
[NotNull] RouteDirection routeDirection,
[NotNull] ILogger logger)
IDictionary<string, object> routeValues,
HttpContext httpContext,
IRouter route,
RouteDirection routeDirection,
ILogger logger)
{
if (routeValues == null)
{
throw new ArgumentNullException(nameof(routeValues));
}
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (route == null)
{
throw new ArgumentNullException(nameof(route));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (constraints == null)
{
return true;

View File

@ -3,7 +3,6 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -28,9 +27,13 @@ namespace Microsoft.AspNet.Routing
{
return _routeData;
}
[param: NotNull]
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(RouteData));
}
_routeData = value;
}
}

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -26,8 +25,13 @@ namespace Microsoft.AspNet.Routing
/// Creates a new <see cref="RouteData"/> instance with values copied from <paramref name="other"/>.
/// </summary>
/// <param name="other">The other <see cref="RouteData"/> instance to copy.</param>
public RouteData([NotNull] RouteData other)
public RouteData(RouteData other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
DataTokens = new Dictionary<string, object>(other.DataTokens, StringComparer.OrdinalIgnoreCase);
Routers = new List<IRouter>(other.Routers);
Values = new RouteValueDictionary(other.Values);

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -28,9 +27,13 @@ namespace Microsoft.AspNet.Routing
{
return _constraintTypeMap;
}
[param: NotNull]
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(ConstraintMap));
}
_constraintTypeMap = value;
}
}

View File

@ -6,7 +6,6 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -85,10 +84,15 @@ namespace Microsoft.AspNet.Routing
}
/// <inheritdoc />
public object this[[NotNull] string key]
public object this[string key]
{
get
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
object value;
_dictionary.TryGetValue(key, out value);
return value;
@ -96,6 +100,11 @@ namespace Microsoft.AspNet.Routing
set
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
_dictionary[key] = value;
}
}
@ -191,8 +200,13 @@ namespace Microsoft.AspNet.Routing
}
/// <inheritdoc />
public void Add([NotNull] string key, object value)
public void Add(string key, object value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
_dictionary.Add(key, value);
}
@ -209,16 +223,26 @@ namespace Microsoft.AspNet.Routing
}
/// <inheritdoc />
public bool ContainsKey([NotNull] string key)
public bool ContainsKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _dictionary.ContainsKey(key);
}
/// <inheritdoc />
void ICollection<KeyValuePair<string, object>>.CopyTo(
[NotNull] KeyValuePair<string, object>[] array,
KeyValuePair<string, object>[] array,
int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
((ICollection<KeyValuePair<string, object>>)_dictionary).CopyTo(array, arrayIndex);
}
@ -247,14 +271,24 @@ namespace Microsoft.AspNet.Routing
}
/// <inheritdoc />
public bool Remove([NotNull] string key)
public bool Remove(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _dictionary.Remove(key);
}
/// <inheritdoc />
public bool TryGetValue([NotNull] string key, out object value)
public bool TryGetValue(string key, out object value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _dictionary.TryGetValue(key, out value);
}
}

View File

@ -3,7 +3,6 @@
using System;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
@ -19,8 +18,13 @@ namespace Microsoft.Framework.DependencyInjection
/// <param name="setupAction">An action to configure the <see cref="RouteOptions"/>.</param>
public static void ConfigureRouting(
this IServiceCollection services,
[NotNull] Action<RouteOptions> setupAction)
Action<RouteOptions> setupAction)
{
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
services.Configure(setupAction);
}
}

View File

@ -1,7 +1,7 @@
// 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 Microsoft.Framework.Internal;
using System;
namespace Microsoft.AspNet.Routing.Template
{
@ -14,8 +14,13 @@ namespace Microsoft.AspNet.Routing.Template
/// Creates a new <see cref="InlineConstraint"/>.
/// </summary>
/// <param name="constraint">The constraint text.</param>
public InlineConstraint([NotNull] string constraint)
public InlineConstraint(string constraint)
{
if (constraint == null)
{
throw new ArgumentNullException(nameof(constraint));
}
Constraint = constraint;
}

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Template
{
@ -14,8 +13,13 @@ namespace Microsoft.AspNet.Routing.Template
{
private const string SeparatorString = "/";
public RouteTemplate([NotNull] List<TemplateSegment> segments)
public RouteTemplate(List<TemplateSegment> segments)
{
if (segments == null)
{
throw new ArgumentNullException(nameof(segments));
}
Segments = segments;
Parameters = new List<TemplatePart>();

View File

@ -8,7 +8,6 @@ using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Http.Extensions;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Template
{
@ -17,8 +16,13 @@ namespace Microsoft.AspNet.Routing.Template
private readonly IReadOnlyDictionary<string, object> _defaults;
private readonly RouteTemplate _template;
public TemplateBinder([NotNull] RouteTemplate template, IReadOnlyDictionary<string, object> defaults)
public TemplateBinder(RouteTemplate template, IReadOnlyDictionary<string, object> defaults)
{
if (template == null)
{
throw new ArgumentNullException(nameof(template));
}
_template = template;
_defaults = defaults;
}
@ -216,7 +220,7 @@ namespace Microsoft.AspNet.Routing.Template
// we won't necessarily add it to the URI we generate.
if (!context.Buffer(converted))
{
return null;
return null;
}
}
else
@ -353,8 +357,13 @@ namespace Microsoft.AspNet.Routing.Template
public TemplateBindingContext(
IReadOnlyDictionary<string, object> defaults,
[NotNull] IDictionary<string, object> values)
IDictionary<string, object> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
_defaults = defaults;
_acceptedValues = new RouteValueDictionary();

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Template
{
@ -16,9 +15,19 @@ namespace Microsoft.AspNet.Routing.Template
private static readonly char[] Delimiters = new char[] { SeparatorChar };
public TemplateMatcher(
[NotNull] RouteTemplate template,
[NotNull] IReadOnlyDictionary<string, object> defaults)
RouteTemplate template,
IReadOnlyDictionary<string, object> defaults)
{
if (template == null)
{
throw new ArgumentNullException(nameof(template));
}
if (defaults == null)
{
throw new ArgumentNullException(nameof(defaults));
}
Template = template;
Defaults = defaults ?? RouteValueDictionary.Empty;
}
@ -27,8 +36,13 @@ namespace Microsoft.AspNet.Routing.Template
public RouteTemplate Template { get; private set; }
public IDictionary<string, object> Match([NotNull] string requestPath)
public IDictionary<string, object> Match(string requestPath)
{
if (requestPath == null)
{
throw new ArgumentNullException(nameof(requestPath));
}
var requestSegments = requestPath.Split(Delimiters);
var values = new RouteValueDictionary();
@ -182,7 +196,7 @@ namespace Microsoft.AspNet.Routing.Template
// In this case we start again from p2 to match the request and we succeed giving
// the value bar to p2
if (routeSegment.Parts[indexOfLastSegment].IsOptional &&
routeSegment.Parts[indexOfLastSegment - 1].IsOptionalSeperator)
routeSegment.Parts[indexOfLastSegment - 1].IsOptionalSeperator)
{
if (MatchComplexSegmentCore(routeSegment, requestSegment, Defaults, values, indexOfLastSegment))
{
@ -220,7 +234,7 @@ namespace Microsoft.AspNet.Routing.Template
// Find last literal segment and get its last index in the string
var lastIndex = requestSegment.Length;
TemplatePart parameterNeedsValue = null; // Keeps track of a parameter segment that is pending a value
TemplatePart lastLiteral = null; // Keeps track of the left-most literal we've encountered
@ -234,7 +248,7 @@ namespace Microsoft.AspNet.Routing.Template
if (part.IsParameter)
{
// Hold on to the parameter so that we can fill it in when we locate the next literal
parameterNeedsValue = part;
parameterNeedsValue = part;
}
else
{
@ -256,10 +270,10 @@ namespace Microsoft.AspNet.Routing.Template
var indexOfLiteral = requestSegment.LastIndexOf(part.Text,
startIndex,
StringComparison.OrdinalIgnoreCase);
if (indexOfLiteral == -1)
if (indexOfLiteral == -1)
{
// If we couldn't find this literal index, this segment cannot match
return false;
return false;
}
// If the first subsegment is a literal, it must match at the right-most extent of the request URI.
@ -320,7 +334,7 @@ namespace Microsoft.AspNet.Routing.Template
// For these segments all parameters must have non-empty values. If the parameter
// has an empty value it's not a match.
return false;
}
else
{

View File

@ -1,10 +1,10 @@
// 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 System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing.Template
{
@ -20,12 +20,17 @@ namespace Microsoft.AspNet.Routing.Template
};
}
public static TemplatePart CreateParameter([NotNull] string name,
public static TemplatePart CreateParameter(string name,
bool isCatchAll,
bool isOptional,
object defaultValue,
IEnumerable<InlineConstraint> inlineConstraints)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return new TemplatePart()
{
IsParameter = true,

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Routing.Template
@ -25,36 +24,44 @@ namespace Microsoft.AspNet.Routing.Template
private ILogger _constraintLogger;
public TemplateRoute(
[NotNull] IRouter target,
IRouter target,
string routeTemplate,
IInlineConstraintResolver inlineConstraintResolver)
: this(target,
routeTemplate,
defaults: null,
constraints: null,
dataTokens: null,
inlineConstraintResolver: inlineConstraintResolver)
: this(
target,
routeTemplate,
defaults: null,
constraints: null,
dataTokens: null,
inlineConstraintResolver: inlineConstraintResolver)
{
}
public TemplateRoute([NotNull] IRouter target,
string routeTemplate,
IDictionary<string, object> defaults,
IDictionary<string, object> constraints,
IDictionary<string, object> dataTokens,
IInlineConstraintResolver inlineConstraintResolver)
public TemplateRoute(
IRouter target,
string routeTemplate,
IDictionary<string, object> defaults,
IDictionary<string, object> constraints,
IDictionary<string, object> dataTokens,
IInlineConstraintResolver inlineConstraintResolver)
: this(target, null, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
{
}
public TemplateRoute([NotNull] IRouter target,
string routeName,
string routeTemplate,
IDictionary<string, object> defaults,
IDictionary<string, object> constraints,
IDictionary<string, object> dataTokens,
IInlineConstraintResolver inlineConstraintResolver)
public TemplateRoute(
IRouter target,
string routeName,
string routeTemplate,
IDictionary<string, object> defaults,
IDictionary<string, object> constraints,
IDictionary<string, object> dataTokens,
IInlineConstraintResolver inlineConstraintResolver)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
_target = target;
_routeTemplate = routeTemplate ?? string.Empty;
Name = routeName;
@ -94,8 +101,13 @@ namespace Microsoft.AspNet.Routing.Template
get { return _constraints; }
}
public async virtual Task RouteAsync([NotNull] RouteContext context)
public async virtual Task RouteAsync(RouteContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
EnsureLoggers(context.HttpContext);
var requestPath = context.HttpContext.Request.Path.Value;

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Routing
{
@ -21,7 +20,7 @@ namespace Microsoft.AspNet.Routing
/// </summary>
/// <param name="router">The object that is used to generate the URL.</param>
/// <param name="virtualPath">The generated URL.</param>
public VirtualPathData([NotNull] IRouter router, string virtualPath)
public VirtualPathData(IRouter router, string virtualPath)
: this(router, virtualPath, dataTokens: new RouteValueDictionary())
{
}
@ -33,11 +32,10 @@ namespace Microsoft.AspNet.Routing
/// <param name="virtualPath">The generated URL.</param>
/// <param name="dataTokens">The collection of custom values.</param>
public VirtualPathData(
[NotNull] IRouter router,
IRouter router,
string virtualPath,
IDictionary<string, object> dataTokens)
: this(router, CreatePathString(virtualPath), dataTokens)
: this(router, CreatePathString(virtualPath), dataTokens)
{
}
@ -48,10 +46,15 @@ namespace Microsoft.AspNet.Routing
/// <param name="virtualPath">The generated URL.</param>
/// <param name="dataTokens">The collection of custom values.</param>
public VirtualPathData(
[NotNull] IRouter router,
IRouter router,
PathString virtualPath,
IDictionary<string, object> dataTokens)
{
if (router == null)
{
throw new ArgumentNullException(nameof(router));
}
Router = router;
VirtualPath = virtualPath;

View File

@ -11,11 +11,7 @@
"dependencies": {
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.Framework.Logging.Abstractions": "1.0.0-*",
"Microsoft.Framework.OptionsModel": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Sources": {
"type": "build",
"version": "1.0.0-*"
}
"Microsoft.Framework.OptionsModel": "1.0.0-*"
},
"frameworks": {
"dnx451": { },

View File

@ -1,12 +1,9 @@
// 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.
#if DNX451
using System.Collections.Generic;
using Microsoft.AspNet.Routing.Constraints;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Routing.Template.Tests
@ -921,7 +918,9 @@ namespace Microsoft.AspNet.Routing.Template.Tests
IDictionary<string, object> expected)
{
// Arrange
var matcher = new TemplateMatcher(TemplateParser.Parse(template), defaults);
var matcher = new TemplateMatcher(
TemplateParser.Parse(template),
defaults ?? new Dictionary<string, object>());
// Act
var match = matcher.Match(path);
@ -951,4 +950,3 @@ namespace Microsoft.AspNet.Routing.Template.Tests
}
}
}
#endif