Replace NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-09-27 00:40:43 -07:00
parent fa5f9399e8
commit 02c36a1c48
17 changed files with 461 additions and 92 deletions

View File

@ -9,9 +9,7 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.AspNet.Mvc.ModelBinding.Validation;
using Microsoft.AspNet.Mvc.Routing;
using Microsoft.AspNet.Mvc.WebApiCompatShim; using Microsoft.AspNet.Mvc.WebApiCompatShim;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -128,8 +126,13 @@ namespace System.Web.Http
/// <param name="message">The user-visible error message.</param> /// <param name="message">The user-visible error message.</param>
/// <returns>A <see cref="BadRequestErrorMessageResult"/> with the specified error message.</returns> /// <returns>A <see cref="BadRequestErrorMessageResult"/> with the specified error message.</returns>
[NonAction] [NonAction]
public virtual BadRequestErrorMessageResult BadRequest([NotNull] string message) public virtual BadRequestErrorMessageResult BadRequest(string message)
{ {
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
return new BadRequestErrorMessageResult(message); return new BadRequestErrorMessageResult(message);
} }
@ -139,8 +142,13 @@ namespace System.Web.Http
/// <param name="modelState">The model state to include in the error.</param> /// <param name="modelState">The model state to include in the error.</param>
/// <returns>An <see cref="InvalidModelStateResult"/> with the specified model state.</returns> /// <returns>An <see cref="InvalidModelStateResult"/> with the specified model state.</returns>
[NonAction] [NonAction]
public virtual InvalidModelStateResult BadRequest([NotNull] ModelStateDictionary modelState) public virtual InvalidModelStateResult BadRequest(ModelStateDictionary modelState)
{ {
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
return new InvalidModelStateResult(modelState, includeErrorDetail: false); return new InvalidModelStateResult(modelState, includeErrorDetail: false);
} }
@ -160,8 +168,13 @@ namespace System.Web.Http
/// <param name="value">The content value to negotiate and format in the entity body.</param> /// <param name="value">The content value to negotiate and format in the entity body.</param>
/// <returns>A <see cref="NegotiatedContentResult{T}"/> with the specified values.</returns> /// <returns>A <see cref="NegotiatedContentResult{T}"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, [NotNull] T value) public virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value)
{ {
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return new NegotiatedContentResult<T>(statusCode, value); return new NegotiatedContentResult<T>(statusCode, value);
} }
@ -174,8 +187,13 @@ namespace System.Web.Http
/// <param name="content">The content value to format in the entity body.</param> /// <param name="content">The content value to format in the entity body.</param>
/// <returns>A <see cref="CreatedResult"/> with the specified values.</returns> /// <returns>A <see cref="CreatedResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual CreatedResult Created([NotNull] string location, object content) public virtual CreatedResult Created(string location, object content)
{ {
if (location == null)
{
throw new ArgumentNullException(nameof(location));
}
return new CreatedResult(location, content); return new CreatedResult(location, content);
} }
@ -186,8 +204,13 @@ namespace System.Web.Http
/// <param name="content">The content value to format in the entity body.</param> /// <param name="content">The content value to format in the entity body.</param>
/// <returns>A <see cref="CreatedResult"/> with the specified values.</returns> /// <returns>A <see cref="CreatedResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual CreatedResult Created([NotNull] Uri uri, object content) public virtual CreatedResult Created(Uri uri, object content)
{ {
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
string location; string location;
if (uri.IsAbsoluteUri) if (uri.IsAbsoluteUri)
{ {
@ -209,10 +232,15 @@ namespace System.Web.Http
/// <returns>A <see cref="CreatedAtRouteResult"/> with the specified values.</returns> /// <returns>A <see cref="CreatedAtRouteResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual CreatedAtRouteResult CreatedAtRoute( public virtual CreatedAtRouteResult CreatedAtRoute(
[NotNull] string routeName, string routeName,
object routeValues, object routeValues,
object content) object content)
{ {
if (routeName == null)
{
throw new ArgumentNullException(nameof(routeName));
}
return new CreatedAtRouteResult(routeName, routeValues, content); return new CreatedAtRouteResult(routeName, routeValues, content);
} }
@ -232,8 +260,13 @@ namespace System.Web.Http
/// <param name="exception">The exception to include in the error.</param> /// <param name="exception">The exception to include in the error.</param>
/// <returns>An <see cref="ExceptionResult"/> with the specified exception.</returns> /// <returns>An <see cref="ExceptionResult"/> with the specified exception.</returns>
[NonAction] [NonAction]
public virtual ExceptionResult InternalServerError([NotNull] Exception exception) public virtual ExceptionResult InternalServerError(Exception exception)
{ {
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
return new ExceptionResult(exception, includeErrorDetail: false); return new ExceptionResult(exception, includeErrorDetail: false);
} }
@ -244,8 +277,13 @@ namespace System.Web.Http
/// <param name="content">The content value to serialize in the entity body.</param> /// <param name="content">The content value to serialize in the entity body.</param>
/// <returns>A <see cref="JsonResult"/> with the specified value.</returns> /// <returns>A <see cref="JsonResult"/> with the specified value.</returns>
[NonAction] [NonAction]
public virtual JsonResult Json<T>([NotNull] T content) public virtual JsonResult Json<T>(T content)
{ {
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
return new JsonResult(content); return new JsonResult(content);
} }
@ -257,8 +295,18 @@ namespace System.Web.Http
/// <param name="serializerSettings">The serializer settings.</param> /// <param name="serializerSettings">The serializer settings.</param>
/// <returns>A <see cref="JsonResult"/> with the specified values.</returns> /// <returns>A <see cref="JsonResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual JsonResult Json<T>([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings) public virtual JsonResult Json<T>(T content, JsonSerializerSettings serializerSettings)
{ {
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
if (serializerSettings == null)
{
throw new ArgumentNullException(nameof(serializerSettings));
}
return new JsonResult(content, serializerSettings); return new JsonResult(content, serializerSettings);
} }
@ -272,10 +320,25 @@ namespace System.Web.Http
/// <returns>A <see cref="JsonResult"/> with the specified values.</returns> /// <returns>A <see cref="JsonResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual JsonResult Json<T>( public virtual JsonResult Json<T>(
[NotNull] T content, T content,
[NotNull] JsonSerializerSettings serializerSettings, JsonSerializerSettings serializerSettings,
[NotNull] Encoding encoding) Encoding encoding)
{ {
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
if (serializerSettings == null)
{
throw new ArgumentNullException(nameof(serializerSettings));
}
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
var result = new JsonResult(content, serializerSettings); var result = new JsonResult(content, serializerSettings);
result.ContentType = new MediaTypeHeaderValue("application/json") result.ContentType = new MediaTypeHeaderValue("application/json")
{ {
@ -323,8 +386,13 @@ namespace System.Web.Http
/// <param name="location">The location to which to redirect.</param> /// <param name="location">The location to which to redirect.</param>
/// <returns>A <see cref="RedirectResult"/> with the specified value.</returns> /// <returns>A <see cref="RedirectResult"/> with the specified value.</returns>
[NonAction] [NonAction]
public virtual RedirectResult Redirect([NotNull] string location) public virtual RedirectResult Redirect(string location)
{ {
if (location == null)
{
throw new ArgumentNullException(nameof(location));
}
// This is how redirect was implemented in legacy webapi - string URIs are assumed to be absolute. // This is how redirect was implemented in legacy webapi - string URIs are assumed to be absolute.
return Redirect(new Uri(location)); return Redirect(new Uri(location));
} }
@ -335,8 +403,13 @@ namespace System.Web.Http
/// <param name="location">The location to which to redirect.</param> /// <param name="location">The location to which to redirect.</param>
/// <returns>A <see cref="RedirectResult"/> with the specified value.</returns> /// <returns>A <see cref="RedirectResult"/> with the specified value.</returns>
[NonAction] [NonAction]
public virtual RedirectResult Redirect([NotNull] Uri location) public virtual RedirectResult Redirect(Uri location)
{ {
if (location == null)
{
throw new ArgumentNullException(nameof(location));
}
string uri; string uri;
if (location.IsAbsoluteUri) if (location.IsAbsoluteUri)
{ {
@ -357,8 +430,18 @@ namespace System.Web.Http
/// <param name="routeValues">The route data to use for generating the URL.</param> /// <param name="routeValues">The route data to use for generating the URL.</param>
/// <returns>A <see cref="RedirectToRouteResult"/> with the specified values.</returns> /// <returns>A <see cref="RedirectToRouteResult"/> with the specified values.</returns>
[NonAction] [NonAction]
public virtual RedirectToRouteResult RedirectToRoute([NotNull] string routeName, [NotNull] object routeValues) public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)
{ {
if (routeName == null)
{
throw new ArgumentNullException(nameof(routeName));
}
if (routeValues == null)
{
throw new ArgumentNullException(nameof(routeValues));
}
return new RedirectToRouteResult(routeName, routeValues) return new RedirectToRouteResult(routeName, routeValues)
{ {
UrlHelper = Url, UrlHelper = Url,
@ -371,8 +454,13 @@ namespace System.Web.Http
/// <param name="response">The HTTP response message.</param> /// <param name="response">The HTTP response message.</param>
/// <returns>A <see cref="ResponseMessageResult"/> for the specified response.</returns> /// <returns>A <see cref="ResponseMessageResult"/> for the specified response.</returns>
[NonAction] [NonAction]
public virtual ResponseMessageResult ResponseMessage([NotNull] HttpResponseMessage response) public virtual ResponseMessageResult ResponseMessage(HttpResponseMessage response)
{ {
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
return new ResponseMessageResult(response); return new ResponseMessageResult(response);
} }

View File

@ -4,7 +4,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Internal;
namespace System.Web.Http namespace System.Web.Http
{ {
@ -16,9 +15,14 @@ namespace System.Web.Http
{ {
/// <summary>Initializes a new instance of the <see cref="BadRequestErrorMessageResult"/> class.</summary> /// <summary>Initializes a new instance of the <see cref="BadRequestErrorMessageResult"/> class.</summary>
/// <param name="message">The user-visible error message.</param> /// <param name="message">The user-visible error message.</param>
public BadRequestErrorMessageResult([NotNull] string message) public BadRequestErrorMessageResult(string message)
: base(new HttpError(message)) : base(new HttpError(message))
{ {
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
Message = message; Message = message;
} }

View File

@ -3,11 +3,8 @@
#if DNXCORE50 #if DNXCORE50
using Microsoft.AspNet.Mvc;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Web.Http;
using Microsoft.Framework.Internal;
namespace System.Net.Http.Formatting namespace System.Net.Http.Formatting
{ {
@ -24,8 +21,13 @@ namespace System.Net.Http.Formatting
/// </summary> /// </summary>
/// <param name="formatter">The formatter.</param> /// <param name="formatter">The formatter.</param>
/// <param name="mediaType">The preferred media type. Can be <c>null</c>.</param> /// <param name="mediaType">The preferred media type. Can be <c>null</c>.</param>
public ContentNegotiationResult([NotNull] MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType) public ContentNegotiationResult(MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType)
{ {
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
_formatter = formatter; _formatter = formatter;
MediaType = mediaType; MediaType = mediaType;
} }

View File

@ -57,10 +57,25 @@ namespace System.Net.Http.Formatting
/// <returns>The result of the negotiation containing the most appropriate <see cref="MediaTypeFormatter"/> /// <returns>The result of the negotiation containing the most appropriate <see cref="MediaTypeFormatter"/>
/// instance, or <c>null</c> if there is no appropriate formatter.</returns> /// instance, or <c>null</c> if there is no appropriate formatter.</returns>
public virtual ContentNegotiationResult Negotiate( public virtual ContentNegotiationResult Negotiate(
[NotNull] Type type, Type type,
[NotNull] HttpRequestMessage request, HttpRequestMessage request,
[NotNull] IEnumerable<MediaTypeFormatter> formatters) IEnumerable<MediaTypeFormatter> formatters)
{ {
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatters == null)
{
throw new ArgumentNullException(nameof(formatters));
}
// Go through each formatter to compute how well it matches. // Go through each formatter to compute how well it matches.
var matches = ComputeFormatterMatches(type, request, formatters); var matches = ComputeFormatterMatches(type, request, formatters);
@ -96,10 +111,25 @@ namespace System.Net.Http.Formatting
/// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param> /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
/// <returns>A collection containing all the matches.</returns> /// <returns>A collection containing all the matches.</returns>
protected virtual Collection<MediaTypeFormatterMatch> ComputeFormatterMatches( protected virtual Collection<MediaTypeFormatterMatch> ComputeFormatterMatches(
[NotNull] Type type, Type type,
[NotNull] HttpRequestMessage request, HttpRequestMessage request,
[NotNull] IEnumerable<MediaTypeFormatter> formatters) IEnumerable<MediaTypeFormatter> formatters)
{ {
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatters == null)
{
throw new ArgumentNullException(nameof(formatters));
}
IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues = null; IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues = null;
// Go through each formatter to find how well it matches. // Go through each formatter to find how well it matches.
@ -159,8 +189,13 @@ namespace System.Net.Http.Formatting
/// <param name="matches">The collection of matches.</param> /// <param name="matches">The collection of matches.</param>
/// <returns>The <see cref="MediaTypeFormatterMatch"/> determined to be the best match.</returns> /// <returns>The <see cref="MediaTypeFormatterMatch"/> determined to be the best match.</returns>
protected virtual MediaTypeFormatterMatch SelectResponseMediaTypeFormatter( protected virtual MediaTypeFormatterMatch SelectResponseMediaTypeFormatter(
[NotNull] ICollection<MediaTypeFormatterMatch> matches) ICollection<MediaTypeFormatterMatch> matches)
{ {
if (matches == null)
{
throw new ArgumentNullException(nameof(matches));
}
// Performance-sensitive // Performance-sensitive
var matchList = matches.AsList(); var matchList = matches.AsList();
@ -269,9 +304,19 @@ namespace System.Net.Http.Formatting
/// </summary> /// </summary>
/// <returns>The <see cref="Encoding"/> determined to be the best match.</returns> /// <returns>The <see cref="Encoding"/> determined to be the best match.</returns>
protected virtual Encoding SelectResponseCharacterEncoding( protected virtual Encoding SelectResponseCharacterEncoding(
[NotNull] HttpRequestMessage request, HttpRequestMessage request,
[NotNull] MediaTypeFormatter formatter) MediaTypeFormatter formatter)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
// If there are any SupportedEncodings then we pick an encoding // If there are any SupportedEncodings then we pick an encoding
var supportedEncodings = formatter.SupportedEncodings.ToList(); var supportedEncodings = formatter.SupportedEncodings.ToList();
if (supportedEncodings.Count > 0) if (supportedEncodings.Count > 0)
@ -311,9 +356,19 @@ namespace System.Net.Http.Formatting
/// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match. /// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match.
/// </returns> /// </returns>
protected virtual MediaTypeFormatterMatch MatchAcceptHeader( protected virtual MediaTypeFormatterMatch MatchAcceptHeader(
[NotNull] IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues, IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues,
[NotNull] MediaTypeFormatter formatter) MediaTypeFormatter formatter)
{ {
if (sortedAcceptValues == null)
{
throw new ArgumentNullException(nameof(sortedAcceptValues));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
foreach (MediaTypeWithQualityHeaderValue acceptMediaTypeValue in sortedAcceptValues) foreach (MediaTypeWithQualityHeaderValue acceptMediaTypeValue in sortedAcceptValues)
{ {
var supportedMediaTypes = formatter.SupportedMediaTypes.ToList(); var supportedMediaTypes = formatter.SupportedMediaTypes.ToList();
@ -347,9 +402,19 @@ namespace System.Net.Http.Formatting
/// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match. /// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match.
/// </returns> /// </returns>
protected virtual MediaTypeFormatterMatch MatchRequestMediaType( protected virtual MediaTypeFormatterMatch MatchRequestMediaType(
[NotNull] HttpRequestMessage request, HttpRequestMessage request,
[NotNull] MediaTypeFormatter formatter) MediaTypeFormatter formatter)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
if (request.Content != null) if (request.Content != null)
{ {
var requestMediaType = request.Content.Headers.ContentType; var requestMediaType = request.Content.Headers.ContentType;
@ -385,8 +450,13 @@ namespace System.Net.Http.Formatting
/// True if not ExcludeMatchOnTypeOnly and accept headers with a q-factor bigger than 0.0 are present. /// True if not ExcludeMatchOnTypeOnly and accept headers with a q-factor bigger than 0.0 are present.
/// </returns> /// </returns>
protected virtual bool ShouldMatchOnType( protected virtual bool ShouldMatchOnType(
[NotNull] IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues) IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues)
{ {
if (sortedAcceptValues == null)
{
throw new ArgumentNullException(nameof(sortedAcceptValues));
}
return !(ExcludeMatchOnTypeOnly && sortedAcceptValues.Any()); return !(ExcludeMatchOnTypeOnly && sortedAcceptValues.Any());
} }
@ -399,9 +469,19 @@ namespace System.Net.Http.Formatting
/// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match. /// A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match.
/// </returns> /// </returns>
protected virtual MediaTypeFormatterMatch MatchType( protected virtual MediaTypeFormatterMatch MatchType(
[NotNull] Type type, Type type,
[NotNull] MediaTypeFormatter formatter) MediaTypeFormatter formatter)
{ {
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
// We already know that we do match on type -- otherwise we wouldn't even be called -- // We already know that we do match on type -- otherwise we wouldn't even be called --
// so this is just a matter of determining how we match. // so this is just a matter of determining how we match.
MediaTypeHeaderValue mediaType = null; MediaTypeHeaderValue mediaType = null;
@ -447,8 +527,13 @@ namespace System.Net.Http.Formatting
/// <param name="headerValues">The header values to sort.</param> /// <param name="headerValues">The header values to sort.</param>
/// <returns>The sorted header values.</returns> /// <returns>The sorted header values.</returns>
protected virtual IEnumerable<StringWithQualityHeaderValue> SortStringWithQualityHeaderValuesByQFactor( protected virtual IEnumerable<StringWithQualityHeaderValue> SortStringWithQualityHeaderValuesByQFactor(
[NotNull] ICollection<StringWithQualityHeaderValue> headerValues) ICollection<StringWithQualityHeaderValue> headerValues)
{ {
if (headerValues == null)
{
throw new ArgumentNullException(nameof(headerValues));
}
if (headerValues.Count > 1) if (headerValues.Count > 1)
{ {
// Use OrderBy() instead of Array.Sort() as it performs fewer comparisons. In this case the comparisons // Use OrderBy() instead of Array.Sort() as it performs fewer comparisons. In this case the comparisons

View File

@ -25,6 +25,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
public void Apply(ControllerModel controller) public void Apply(ControllerModel controller)
{ {
if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}
if (IsConventionApplicable(controller)) if (IsConventionApplicable(controller))
{ {
var newActions = new List<ActionModel>(); var newActions = new List<ActionModel>();

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Mvc.ApplicationModels; using Microsoft.AspNet.Mvc.ApplicationModels;
@ -10,6 +11,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
{ {
public void Apply(ActionModel action) public void Apply(ActionModel action)
{ {
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (IsConventionApplicable(action.Controller)) if (IsConventionApplicable(action.Controller))
{ {
action.ActionConstraints.Add(new OverloadActionConstraint()); action.ActionConstraints.Add(new OverloadActionConstraint());

View File

@ -16,6 +16,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
{ {
public void Apply(ActionModel action) public void Apply(ActionModel action)
{ {
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (IsConventionApplicable(action.Controller)) if (IsConventionApplicable(action.Controller))
{ {
var optionalParameters = new HashSet<string>(); var optionalParameters = new HashSet<string>();

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Mvc.ApplicationModels; using Microsoft.AspNet.Mvc.ApplicationModels;
@ -17,6 +18,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
public void Apply(ControllerModel controller) public void Apply(ControllerModel controller)
{ {
if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}
if (IsConventionApplicable(controller)) if (IsConventionApplicable(controller))
{ {
controller.RouteConstraints.Add(new AreaAttribute(_area)); controller.RouteConstraints.Add(new AreaAttribute(_area));

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http.Formatting; using System.Net.Http.Formatting;
using System.Text; using System.Text;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.WebApiCompatShim namespace Microsoft.AspNet.Mvc.WebApiCompatShim
{ {
@ -80,8 +79,13 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
} }
public static IEnumerable<KeyValuePair<string, string>> GetJQueryNameValuePairs( public static IEnumerable<KeyValuePair<string, string>> GetJQueryNameValuePairs(
[NotNull] this FormDataCollection formData) this FormDataCollection formData)
{ {
if (formData == null)
{
throw new ArgumentNullException(nameof(formData));
}
var count = 0; var count = 0;
foreach (var kv in formData) foreach (var kv in formData)

View File

@ -6,9 +6,7 @@ using System.Linq;
using System.Xml; using System.Xml;
using System.Xml.Schema; using System.Xml.Schema;
using System.Xml.Serialization; using System.Xml.Serialization;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.Framework.Internal;
using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources;
namespace System.Web.Http namespace System.Web.Http
@ -34,9 +32,14 @@ namespace System.Web.Http
/// <paramref name="message"/>. /// <paramref name="message"/>.
/// </summary> /// </summary>
/// <param name="message">The error message to associate with this instance.</param> /// <param name="message">The error message to associate with this instance.</param>
public HttpError([NotNull] string message) public HttpError(string message)
: this() : this()
{ {
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
Message = message; Message = message;
} }
@ -47,9 +50,14 @@ namespace System.Web.Http
/// <param name="includeErrorDetail"> /// <param name="includeErrorDetail">
/// <c>true</c> to include the exception information in the error;<c>false</c> otherwise. /// <c>true</c> to include the exception information in the error;<c>false</c> otherwise.
/// </param> /// </param>
public HttpError([NotNull] Exception exception, bool includeErrorDetail) public HttpError(Exception exception, bool includeErrorDetail)
: this() : this()
{ {
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
Message = ShimResources.HttpError_GenericError; Message = ShimResources.HttpError_GenericError;
if (includeErrorDetail) if (includeErrorDetail)
@ -71,9 +79,14 @@ namespace System.Web.Http
/// <param name="includeErrorDetail"> /// <param name="includeErrorDetail">
/// <c>true</c> to include exception messages in the error; <c>false</c> otherwise. /// <c>true</c> to include exception messages in the error; <c>false</c> otherwise.
/// </param> /// </param>
public HttpError([NotNull] ModelStateDictionary modelState, bool includeErrorDetail) public HttpError(ModelStateDictionary modelState, bool includeErrorDetail)
: this() : this()
{ {
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
if (modelState.IsValid) if (modelState.IsValid)
{ {
throw new ArgumentException(ShimResources.HttpError_ValidModelState, nameof(modelState)); throw new ArgumentException(ShimResources.HttpError_ValidModelState, nameof(modelState));

View File

@ -9,7 +9,6 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.WebApiCompatShim; using Microsoft.AspNet.Mvc.WebApiCompatShim;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources;
@ -37,9 +36,19 @@ namespace System.Net.Http
/// range. /// range.
/// </returns> /// </returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
[NotNull] InvalidByteRangeException invalidByteRangeException) InvalidByteRangeException invalidByteRangeException)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (invalidByteRangeException == null)
{
throw new ArgumentNullException(nameof(invalidByteRangeException));
}
var rangeNotSatisfiableResponse = request.CreateErrorResponse( var rangeNotSatisfiableResponse = request.CreateErrorResponse(
HttpStatusCode.RequestedRangeNotSatisfiable, HttpStatusCode.RequestedRangeNotSatisfiable,
invalidByteRangeException); invalidByteRangeException);
@ -67,10 +76,20 @@ namespace System.Net.Http
/// <paramref name="statusCode"/>. /// <paramref name="statusCode"/>.
/// </returns> /// </returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] string message) string message)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
return request.CreateErrorResponse(statusCode, new HttpError(message)); return request.CreateErrorResponse(statusCode, new HttpError(message));
} }
@ -92,11 +111,26 @@ namespace System.Net.Http
/// <returns>An error response for <paramref name="exception"/> with error message <paramref name="message"/> /// <returns>An error response for <paramref name="exception"/> with error message <paramref name="message"/>
/// and status code <paramref name="statusCode"/>.</returns> /// and status code <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] string message, string message,
[NotNull] Exception exception) Exception exception)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
var error = new HttpError(exception, includeErrorDetail: false) { Message = message }; var error = new HttpError(exception, includeErrorDetail: false) { Message = message };
return request.CreateErrorResponse(statusCode, error); return request.CreateErrorResponse(statusCode, error);
} }
@ -118,10 +152,20 @@ namespace System.Net.Http
/// An error response for <paramref name="exception"/> with status code <paramref name="statusCode"/>. /// An error response for <paramref name="exception"/> with status code <paramref name="statusCode"/>.
/// </returns> /// </returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] Exception exception) Exception exception)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
return request.CreateErrorResponse(statusCode, new HttpError(exception, includeErrorDetail: false)); return request.CreateErrorResponse(statusCode, new HttpError(exception, includeErrorDetail: false));
} }
@ -142,10 +186,20 @@ namespace System.Net.Http
/// An error response for <paramref name="modelState"/> with status code <paramref name="statusCode"/>. /// An error response for <paramref name="modelState"/> with status code <paramref name="statusCode"/>.
/// </returns> /// </returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] ModelStateDictionary modelState) ModelStateDictionary modelState)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
return request.CreateErrorResponse(statusCode, new HttpError(modelState, includeErrorDetail: false)); return request.CreateErrorResponse(statusCode, new HttpError(modelState, includeErrorDetail: false));
} }
@ -165,10 +219,20 @@ namespace System.Net.Http
/// An error response wrapping <paramref name="error"/> with status code <paramref name="statusCode"/>. /// An error response wrapping <paramref name="error"/> with status code <paramref name="statusCode"/>.
/// </returns> /// </returns>
public static HttpResponseMessage CreateErrorResponse( public static HttpResponseMessage CreateErrorResponse(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] HttpError error) HttpError error)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (error == null)
{
throw new ArgumentNullException(nameof(error));
}
return request.CreateResponse<HttpError>(statusCode, error); return request.CreateResponse<HttpError>(statusCode, error);
} }
@ -188,8 +252,18 @@ namespace System.Net.Http
/// <returns> /// <returns>
/// A response wrapping <paramref name="value"/> with <see cref="System.Net.HttpStatusCode.OK"/> status code. /// A response wrapping <paramref name="value"/> with <see cref="System.Net.HttpStatusCode.OK"/> status code.
/// </returns> /// </returns>
public static HttpResponseMessage CreateResponse<T>([NotNull] this HttpRequestMessage request, T value) public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, T value)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return request.CreateResponse<T>(HttpStatusCode.OK, value, formatters: null); return request.CreateResponse<T>(HttpStatusCode.OK, value, formatters: null);
} }
@ -232,11 +306,16 @@ namespace System.Net.Http
/// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param> /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
/// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateResponse<T>( public static HttpResponseMessage CreateResponse<T>(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
T value, T value,
IEnumerable<MediaTypeFormatter> formatters) IEnumerable<MediaTypeFormatter> formatters)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var context = GetHttpContext(request); var context = GetHttpContext(request);
if (formatters == null) if (formatters == null)
@ -296,11 +375,26 @@ namespace System.Net.Http
/// </param> /// </param>
/// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateResponse<T>( public static HttpResponseMessage CreateResponse<T>(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] T value, T value,
[NotNull] MediaTypeHeaderValue mediaType) MediaTypeHeaderValue mediaType)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (mediaType == null)
{
throw new ArgumentNullException(nameof(mediaType));
}
var context = GetHttpContext(request); var context = GetHttpContext(request);
// Get the default formatters from options // Get the default formatters from options
@ -330,11 +424,26 @@ namespace System.Net.Http
/// <param name="formatter">The formatter to use.</param> /// <param name="formatter">The formatter to use.</param>
/// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateResponse<T>( public static HttpResponseMessage CreateResponse<T>(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] T value, T value,
[NotNull] MediaTypeFormatter formatter) MediaTypeFormatter formatter)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
return request.CreateResponse(statusCode, value, formatter, (MediaTypeHeaderValue)null); return request.CreateResponse(statusCode, value, formatter, (MediaTypeHeaderValue)null);
} }
@ -352,12 +461,27 @@ namespace System.Net.Http
/// </param> /// </param>
/// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateResponse<T>( public static HttpResponseMessage CreateResponse<T>(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
[NotNull] T value, T value,
[NotNull] MediaTypeFormatter formatter, MediaTypeFormatter formatter,
string mediaType) string mediaType)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
var mediaTypeHeader = mediaType != null ? new MediaTypeHeaderValue(mediaType) : null; var mediaTypeHeader = mediaType != null ? new MediaTypeHeaderValue(mediaType) : null;
return request.CreateResponse(statusCode, value, formatter, mediaTypeHeader); return request.CreateResponse(statusCode, value, formatter, mediaTypeHeader);
} }
@ -376,12 +500,22 @@ namespace System.Net.Http
/// </param> /// </param>
/// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns> /// <returns>A response wrapping <paramref name="value"/> with <paramref name="statusCode"/>.</returns>
public static HttpResponseMessage CreateResponse<T>( public static HttpResponseMessage CreateResponse<T>(
[NotNull] this HttpRequestMessage request, this HttpRequestMessage request,
HttpStatusCode statusCode, HttpStatusCode statusCode,
T value, T value,
[NotNull] MediaTypeFormatter formatter, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType) MediaTypeHeaderValue mediaType)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
var response = new HttpResponseMessage(statusCode) var response = new HttpResponseMessage(statusCode)
{ {
RequestMessage = request, RequestMessage = request,

View File

@ -1,11 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Http; using System.Net.Http;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.WebApiCompatShim namespace Microsoft.AspNet.Mvc.WebApiCompatShim
{ {
@ -14,8 +14,13 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
private readonly HttpContext _httpContext; private readonly HttpContext _httpContext;
private HttpRequestMessage _httpRequestMessage; private HttpRequestMessage _httpRequestMessage;
public HttpRequestMessageFeature([NotNull] HttpContext httpContext) public HttpRequestMessageFeature(HttpContext httpContext)
{ {
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
_httpContext = httpContext; _httpContext = httpContext;
} }

View File

@ -3,8 +3,6 @@
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Internal;
using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources;
namespace System.Web.Http namespace System.Web.Http
@ -24,9 +22,14 @@ namespace System.Web.Http
/// Initializes a new instance of the <see cref="HttpResponseException"/> class. /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
/// </summary> /// </summary>
/// <param name="response">The response message.</param> /// <param name="response">The response message.</param>
public HttpResponseException([NotNull] HttpResponseMessage response) public HttpResponseException(HttpResponseMessage response)
: base(ShimResources.HttpResponseExceptionMessage) : base(ShimResources.HttpResponseExceptionMessage)
{ {
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
Response = response; Response = response;
} }

View File

@ -1,10 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net.Http; using System.Net.Http;
using System.Web.Http; using System.Web.Http;
using Microsoft.AspNet.Mvc.Filters; using Microsoft.AspNet.Mvc.Filters;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.WebApiCompatShim namespace Microsoft.AspNet.Mvc.WebApiCompatShim
{ {
@ -18,12 +18,17 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
// Return a high number by default so that it runs closest to the action. // Return a high number by default so that it runs closest to the action.
public int Order { get; set; } = int.MaxValue - 10; public int Order { get; set; } = int.MaxValue - 10;
public void OnActionExecuting([NotNull] ActionExecutingContext context) public void OnActionExecuting(ActionExecutingContext context)
{ {
} }
public void OnActionExecuted([NotNull] ActionExecutedContext context) public void OnActionExecuted(ActionExecutedContext context)
{ {
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var httpResponseException = context.Exception as HttpResponseException; var httpResponseException = context.Exception as HttpResponseException;
if (httpResponseException != null) if (httpResponseException != null)
{ {

View File

@ -5,7 +5,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.Framework.Internal;
namespace System.Web.Http namespace System.Web.Http
{ {
@ -20,9 +19,14 @@ namespace System.Web.Http
/// <param name="includeErrorDetail"> /// <param name="includeErrorDetail">
/// <see langword="true"/> if the error should include exception messages; otherwise, <see langword="false"/>. /// <see langword="true"/> if the error should include exception messages; otherwise, <see langword="false"/>.
/// </param> /// </param>
public InvalidModelStateResult([NotNull] ModelStateDictionary modelState, bool includeErrorDetail) public InvalidModelStateResult(ModelStateDictionary modelState, bool includeErrorDetail)
: base(new HttpError(modelState, includeErrorDetail)) : base(new HttpError(modelState, includeErrorDetail))
{ {
if (modelState == null)
{
throw new ArgumentNullException(nameof(modelState));
}
ModelState = modelState; ModelState = modelState;
IncludeErrorDetail = includeErrorDetail; IncludeErrorDetail = includeErrorDetail;
} }

View File

@ -3,7 +3,6 @@
using System.Net.Http; using System.Net.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Internal;
namespace System.Web.Http namespace System.Web.Http
{ {
@ -16,9 +15,14 @@ namespace System.Web.Http
/// Initializes a new instance of the <see cref="ResponseMessageResult"/> class. /// Initializes a new instance of the <see cref="ResponseMessageResult"/> class.
/// </summary> /// </summary>
/// <param name="response">The response message.</param> /// <param name="response">The response message.</param>
public ResponseMessageResult([NotNull] HttpResponseMessage response) public ResponseMessageResult(HttpResponseMessage response)
: base(response) : base(response)
{ {
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
Response = response; Response = response;
} }

View File

@ -6,7 +6,7 @@
"url": "git://github.com/aspnet/mvc" "url": "git://github.com/aspnet/mvc"
}, },
"compilationOptions": { "compilationOptions": {
"warningsAsErrors": false "warningsAsErrors": true
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.WebUtilities": "1.0.0-*", "Microsoft.AspNet.WebUtilities": "1.0.0-*",
@ -16,10 +16,6 @@
"Microsoft.Framework.PropertyHelper.Sources": { "Microsoft.Framework.PropertyHelper.Sources": {
"version": "1.0.0-*", "version": "1.0.0-*",
"type": "build" "type": "build"
},
"Microsoft.Framework.NotNullAttribute.Sources": {
"version": "1.0.0-*",
"type": "build"
} }
}, },
"frameworks": { "frameworks": {