0

I have an extension method using Action in ASP.NET Core 3.1:

public static IApplicationBuilder UseSitemap(this IApplicationBuilder builder, Action<(String BaseUrl, String Route)> options) {

  (String BaseUrl, String Route) sitemapOptions = ("", "/sitemap"); // Default values

  options?.Invoke(sitemapOptions);

  return builder.MapWhen(x => x.Request.Path.StartsWithSegments(sitemapOptions.Route), x => x.UseMiddleware<SitemapMiddleware>(sitemapOptions));

}

Then I use it as follows:

  application.UseSitemap(x => { 
    x.BaseUrl = "https://locahost:80";
    x.Route = "/sitemap.xml"; 
  });

However, sitemapOptions is not getting the new values. Why?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

1

(String BaseUrl, String Route) is actually a struct ValueTuple<T1,T2>, so it is passed by value to your options action and can't be updated this way:

public void Update((String BaseUrl, String Route) x)
{
    x.BaseUrl = "https://locahost:80";
    x.Route = "/sitemap.xml";
}

public void Update(ref (String BaseUrl, String Route) x)
{
    x.BaseUrl = "https://locahost:80";
    x.Route = "/sitemap.xml";
}


Update(sitemapOptions);
Console.WriteLine(sitemapOptions); // prints "(, /sitemap)"

Update(ref sitemapOptions);
Console.WriteLine(sitemapOptions); // prints "(https://locahost:80, /sitemap.xml)"
Guru Stron
  • 102,774
  • 10
  • 95
  • 132