Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Default StatusCode for void return type changed to 200 #5369

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ private IReadOnlyList<ApiResponseType> GetApiResponseTypes(

// Set the default status only when no status has already been set explicitly
if (objectTypes.Count == 0
&& type != null
&& type != typeof(void))
&& type != null)
{
objectTypes[StatusCodes.Status200OK] = type;
}
Expand Down
94 changes: 94 additions & 0 deletions src/Microsoft.AspNetCore.Mvc.Core/AcceptedAtActionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// An <see cref="ActionResult"/> that returns a Accepted (202) response with a Location header.
/// </summary>
public class AcceptedAtActionResult : ObjectResult
{
/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtActionResult"/> with the values
/// provided.
/// </summary>
/// <param name="actionName">The name of the action to use for generating the URL.</param>
/// <param name="controllerName">The name of the controller to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedAtActionResult(
string actionName,
string controllerName,
object routeValues,
object value)
: base(value)
{
ActionName = actionName;
ControllerName = controllerName;
RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues);
StatusCode = StatusCodes.Status202Accepted;
}

/// <summary>
/// Gets or sets the <see cref="IUrlHelper" /> used to generate URLs.
/// </summary>
public IUrlHelper UrlHelper { get; set; }

/// <summary>
/// Gets or sets the name of the action to use for generating the URL.
/// </summary>
public string ActionName { get; set; }

/// <summary>
/// Gets or sets the name of the controller to use for generating the URL.
/// </summary>
public string ControllerName { get; set; }

/// <summary>
/// Gets or sets the route data to use for generating the URL.
/// </summary>
public RouteValueDictionary RouteValues { get; set; }

/// <inheritdoc />
public override void OnFormatting(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

base.OnFormatting(context);

var request = context.HttpContext.Request;

var urlHelper = UrlHelper;
if (urlHelper == null)
{
var services = context.HttpContext.RequestServices;
urlHelper = services.GetRequiredService<IUrlHelperFactory>().GetUrlHelper(context);
}

var url = urlHelper.Action(
ActionName,
ControllerName,
RouteValues,
request.Scheme,
request.Host.ToUriComponent());

if (string.IsNullOrEmpty(url))
{
throw new InvalidOperationException(Resources.NoRoutesMatched);
}

context.HttpContext.Response.Headers[HeaderNames.Location] = url;
}
}
}
90 changes: 90 additions & 0 deletions src/Microsoft.AspNetCore.Mvc.Core/AcceptedAtRouteResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// An <see cref="ActionResult"/> that returns a Accepted (202) response with a Location header.
/// </summary>
public class AcceptedAtRouteResult : ObjectResult
{
/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
/// provided.
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedAtRouteResult(object routeValues, object value)
: this(routeName: null, routeValues: routeValues, value: value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedAtRouteResult(
string routeName,
object routeValues,
object value)
: base(value)
{
RouteName = routeName;
RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues);
StatusCode = StatusCodes.Status202Accepted;
}

/// <summary>
/// Gets or sets the <see cref="IUrlHelper" /> used to generate URLs.
/// </summary>
public IUrlHelper UrlHelper { get; set; }

/// <summary>
/// Gets or sets the name of the route to use for generating the URL.
/// </summary>
public string RouteName { get; set; }

/// <summary>
/// Gets or sets the route data to use for generating the URL.
/// </summary>
public RouteValueDictionary RouteValues { get; set; }

/// <inheritdoc />
public override void OnFormatting(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

base.OnFormatting(context);

var urlHelper = UrlHelper;
if (urlHelper == null)
{
var services = context.HttpContext.RequestServices;
urlHelper = services.GetRequiredService<IUrlHelperFactory>().GetUrlHelper(context);
}

var url = urlHelper.Link(RouteName, RouteValues);

if (string.IsNullOrEmpty(url))
{
throw new InvalidOperationException(Resources.NoRoutesMatched);
}

context.HttpContext.Response.Headers[HeaderNames.Location] = url;
}
}
}
80 changes: 80 additions & 0 deletions src/Microsoft.AspNetCore.Mvc.Core/AcceptedResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// An <see cref="ActionResult"/> that returns an Accepted (202) response with a Location header.
/// </summary>
public class AcceptedResult : ObjectResult
{
private string _location;

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedResult"/> class with the values
/// provided.
/// </summary>
/// <param name="location">The location at which the status of requested content can be monitored.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedResult(string location, object value)
: base(value)
{
Location = location;
StatusCode = StatusCodes.Status202Accepted;
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedResult"/> class with the values
/// provided.
/// </summary>
/// <param name="location">The location at which the status of requested content can be monitored
/// It is an optional paramater and may be null</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedResult(Uri location, object value)
: base(value)
{
if (location.IsAbsoluteUri)
{
Location = location.AbsoluteUri;
}
else
{
Location = location.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
}

StatusCode = StatusCodes.Status202Accepted;
}

/// <summary>
/// Gets or sets the location at which the status of the requested content can be monitored.
/// </summary>
public string Location
{
get
{
return _location;
}
set
{
_location = value;
}
}

/// <inheritdoc />
public override void OnFormatting(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

base.OnFormatting(context);

context.HttpContext.Response.Headers[HeaderNames.Location] = Location;
}
}
}
Loading