Browse Source

Update code docs for DynamicRouteValueTransformer (#36653)

The docs aren't entirely clear on what values to return from TransformAsync. After looking at examples posted within issues and PRs in this repo, reading the comments of MS's devs and looking at the code of how the result of TransformAsync works - it seems that a `null` value being returned is a requirement to indicate that your `TransformAsync` didn't match anything. If you do not return `null`. There's a specific check for `null` from this result here https://github.com/dotnet/aspnetcore/blob/8b30d862de6c9146f466061d51aa3f1414ee2337/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs#L129 and having a look at all of Orchards implementations of DynamicRouteValueTransformer, they also return null whenever there is not a match.  I did some further testing and it seems it may be valid to just return the original `values` too since if these values don't match any endpoints, the same logic would be executed as if a null response was there: https://github.com/dotnet/aspnetcore/blob/8b30d862de6c9146f466061d51aa3f1414ee2337/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs#L148 but seems that a `null` value to indicate there was no match is safer and would perform better too to avoid the endpoint lookups.

Co-authored-by: Pranav K <[email protected]>
Shannon Deminick 4 years ago
parent
commit
6cfbdc197b
1 changed files with 1 additions and 1 deletions
  1. 1 1
      src/Mvc/Mvc.Core/src/Routing/DynamicRouteValueTransformer.cs

+ 1 - 1
src/Mvc/Mvc.Core/src/Routing/DynamicRouteValueTransformer.cs

@@ -58,7 +58,7 @@ public abstract class DynamicRouteValueTransformer
     /// </summary>
     /// <param name="httpContext">The <see cref="HttpContext" /> associated with the current request.</param>
     /// <param name="values">The route values associated with the current match. Implementations should not modify <paramref name="values"/>.</param>
-    /// <returns>A task which asynchronously returns a set of route values.</returns>
+    /// <returns>Returns a set of new route values, else null to indicate there was no match.</returns>
     public abstract ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values);
 
     /// <summary>