This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new attribute ProducesResponseTypeAttribute to enable ApiExplor…
…er to expose response type and StatusCode. [Fixes #4101] StatusCode Metadata
- Loading branch information
Showing
11 changed files
with
558 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Microsoft.AspNetCore.Mvc.Core/ProducesResponseTypeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
|
||
namespace Microsoft.AspNetCore.Mvc | ||
{ | ||
/// <summary> | ||
/// Specifies the the type of the value and status code returned by the action | ||
/// which can be used to select a formatter while executing <see cref="ObjectResult"/>. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | ||
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider, IFilterMetadata | ||
{ | ||
/// <summary> | ||
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>. | ||
/// </summary> | ||
/// <param name="type">The <see cref="Type"/> of object that is going to be written in the response.</param> | ||
/// <param name="statusCode">Http response status code</param> | ||
public ProducesResponseTypeAttribute(Type type, int statusCode) | ||
{ | ||
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
Type = type; | ||
StatusCode = statusCode; | ||
} | ||
|
||
/// <summary> | ||
/// The type of the value returned by an action. | ||
/// </summary> | ||
public Type Type { get; set; } | ||
|
||
/// <summary> | ||
/// Http status code response. | ||
/// </summary> | ||
public int StatusCode { get; set; } | ||
|
||
public void SetContentTypes(MediaTypeCollection contentTypes) | ||
{ | ||
// Users are supposed to use the 'Produces' attribute to set the content types that an aciton can support. | ||
} | ||
} | ||
} |
Oops, something went wrong.