Replace ProblemDetailsAttribute with ApiControllerAttribute

This commit is contained in:
Pranav K 2017-09-22 09:49:34 -07:00
parent 97fab8711a
commit 5d1603c37f
6 changed files with 13 additions and 28 deletions

View File

@ -5,15 +5,16 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
{
public class ProblemDetailsApiDescriptionProvider : IApiDescriptionProvider
public class ApiControllerApiDescriptionProvider : IApiDescriptionProvider
{
private readonly IModelMetadataProvider _modelMetadaProvider;
public ProblemDetailsApiDescriptionProvider(IModelMetadataProvider modelMetadataProvider)
public ApiControllerApiDescriptionProvider(IModelMetadataProvider modelMetadataProvider)
{
_modelMetadaProvider = modelMetadataProvider;
}
@ -31,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
{
foreach (var apiDescription in context.Results)
{
if (!apiDescription.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is ProblemDetailsAttribute))
if (!apiDescription.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is IApiBehaviorMetadata))
{
continue;
}

View File

@ -27,7 +27,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, ProblemDetailsApiDescriptionProvider>());
ServiceDescriptor.Transient<IApiDescriptionProvider, ApiControllerApiDescriptionProvider>());
}
}
}

View File

@ -1,16 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// Adds an <see cref="IFilterMetadata"/> that indicates to the framework that the current action conforms to well-known API behavior.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ProblemDetailsAttribute : Attribute, IFilterMetadata
{
}
}

View File

@ -1069,7 +1069,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public async Task ProblemDetails_AddsProblemAsDefaultErrorResult()
{
// Act
var body = await Client.GetStringAsync("ApiExplorerProblemDetails/ActionWithoutParameters");
var body = await Client.GetStringAsync("ApiExplorerApiController/ActionWithoutParameters");
var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body);
// Assert
@ -1087,7 +1087,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public async Task ProblemDetails_AddsProblemAsErrorResultForBadResult_WhenActionHasParameters()
{
// Act
var body = await Client.GetStringAsync("ApiExplorerProblemDetails/ActionWithSomeParameters");
var body = await Client.GetStringAsync("ApiExplorerApiController/ActionWithSomeParameters");
var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body);
// Assert
@ -1108,8 +1108,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
}
[Theory]
[InlineData("ApiExplorerProblemDetails/ActionWithIdParameter")]
[InlineData("ApiExplorerProblemDetails/ActionWithIdSuffixParameter")]
[InlineData("ApiExplorerApiController/ActionWithIdParameter")]
[InlineData("ApiExplorerApiController/ActionWithIdSuffixParameter")]
public async Task ProblemDetails_AddsProblemAsErrorResultForNotFoundResult_WhenActionHasAnIdParameters(string url)
{
// Act

View File

@ -425,7 +425,7 @@ namespace Microsoft.AspNetCore.Mvc
new Type[]
{
typeof(DefaultApiDescriptionProvider),
typeof(ProblemDetailsApiDescriptionProvider),
typeof(ApiControllerApiDescriptionProvider),
typeof(JsonPatchOperationsArrayProvider),
}
},

View File

@ -5,9 +5,9 @@ using Microsoft.AspNetCore.Mvc;
namespace ApiExplorerWebSite
{
[Route("ApiExplorerProblemDetails/[action]")]
[ProblemDetails]
public class ApiExplorerProblemDetailsController : Controller
[Route("ApiExplorerApiController/[action]")]
[ApiController]
public class ApiExplorerApiController : Controller
{
public IActionResult ActionWithoutParameters() => Ok();