Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AcceptedAtRoute link generation and add tests #46724

Merged
merged 2 commits into from
Feb 18, 2023

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Feb 17, 2023

Fixes #46722

Previously AcceptedAtRoute was calling GetUriByAddress, and the address was the route name. This is basically the same as the GetUriByName extension method (see below) and searches for an endpoint by the endpoint name instead of the route name.

/// <summary>
/// Generates an absolute URI based on the provided values.
/// </summary>
/// <param name="generator">The <see cref="LinkGenerator"/>.</param>
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
/// <param name="endpointName">The endpoint name. Used to resolve endpoints.</param>
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
/// <param name="scheme">
/// The URI scheme, applied to the resulting URI. Optional. If not provided, the value of <see cref="HttpRequest.Scheme"/> will be used.
/// </param>
/// <param name="host">
/// The URI host/authority, applied to the resulting URI. Optional. If not provided, the value <see cref="HttpRequest.Host"/> will be used.
/// See the remarks section for details about the security implications of the <paramref name="host"/>.
/// </param>
/// <param name="pathBase">
/// An optional URI path base. Prepended to the path in the resulting URI. If not provided, the value of <see cref="HttpRequest.PathBase"/> will be used.
/// </param>
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
/// <param name="options">
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
/// names from <c>RouteOptions</c>.
/// </param>
/// <returns>A URI with an absolute path, or <c>null</c>.</returns>
/// <remarks>
/// <para>
/// The value of <paramref name="host" /> should be a trusted value. Relying on the value of the current request
/// can allow untrusted input to influence the resulting URI unless the <c>Host</c> header has been validated.
/// See the deployment documentation for instructions on how to properly validate the <c>Host</c> header in
/// your deployment environment.
/// </para>
/// </remarks>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
public static string? GetUriByName(
this LinkGenerator generator,
HttpContext httpContext,
string endpointName,
object? values,
string? scheme = default,
HostString? host = default,
PathString? pathBase = default,
FragmentString fragment = default,
LinkOptions? options = default)
{
ArgumentNullException.ThrowIfNull(generator);
ArgumentNullException.ThrowIfNull(httpContext);
ArgumentNullException.ThrowIfNull(endpointName);
return generator.GetUriByAddress<string>(
httpContext,
endpointName,
new RouteValueDictionary(values),
ambientValues: null,
scheme,
host,
pathBase,
fragment,
options);
}

@JamesNK JamesNK added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Feb 17, 2023
@ghost ghost added the area-runtime label Feb 17, 2023
@JamesNK JamesNK merged commit 320c6a6 into main Feb 18, 2023
@JamesNK JamesNK deleted the jamesnk/result-usecorrectoverload branch February 18, 2023 04:35
@ghost ghost added this to the 8.0-preview2 milestone Feb 18, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Odd difference: AcceptedAtRoute uses GetUriByAddress. CreatedAtRoute uses GetUriByRouteValues.
3 participants