title: How to hook into the Asp.Net Core 6 MVC route system to customize urls generation
body: <p>In the old .Net Framework MVC implementations, I was creating routes by myself so that I could also influence urls generation. Part of the code:</p>
  <pre class="lang-cs prettyprint-override"><code>public class RouteBase : Route
  {
      public RouteBase(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
  
      public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
      {
          if (Url.Contains(&quot;{segment}&quot;) &amp;&amp; !values.ContainsKey(&quot;segment&quot;))
              values[&quot;segment&quot;] = requestContext.HttpContext.Items[&quot;segmentValue&quot;];
  
          return base.GetVirtualPath(requestContext, values);
      }
  }
  </code></pre>
  <p>Thanks to GetVirtualPath, I was able to detect a particular segment in the route template and inject a proper value in the route values dictionary so that the client app did not have to specify it when calling for instance Url.RouteUrl(routeName).</p>
  <p>In asp.net core 6, I'm now using attributes based routing and I don't know how to hook into this so that I can inject some value into the route values dictionary when I generate urls. If I have a route template like so:</p>
  <pre class="lang-cs prettyprint-override"><code>[Route(&quot;{segment}/test&quot;, Name = &quot;name&quot;]
  </code></pre>
  <p>When I call this, I want an injection mechanism from somewhere else in the code so that the known segment value is injected into the route values used to build the url:</p>
  <pre class="lang-cs prettyprint-override"><code>var url = Url.RouteUrl(&quot;name&quot;); // Not passing new { segment = value } as second param
  </code></pre>
  <p>For information, I simply use this in Startup:</p>
  <pre class="lang-cs prettyprint-override"><code>app.MapControllers();
  </code></pre>