title: How to invoke a custom ResultFilter before ClientErrorResultFilter is executed in ASP.NET 6
body: <p>I spent almost a full day debugging why my client can't post any forms, until I found out the anti-forgery mechanism got borked on the client-side and the server just responded with a <code>400</code> error, with zero logs or information (turns out anti-forgery validation is logged internally with <code>Info</code> level).</p>
  <p>So I decided the server needs to special handle this scenario, however <a href="https://stackoverflow.com/a/61201994/6389395">according to this answer</a> I don't really know how to do that (aside from hacking).</p>
  <p>Normally I would set up a <code>IAlwaysRunResultFilter</code> and check for <code>IAntiforgeryValidationFailedResult</code>. Easy.</p>
  <p>Except that I use Api Controllers, so by default all results get transformed into <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.apibehavioroptions.suppressmapclienterrors?view=aspnetcore-6.0" rel="nofollow noreferrer"><code>ProblemDetails</code></a>. So <code>context.Result</code> as <a href="https://stackoverflow.com/q/61074642/6389395">mentioned here</a> is always of type <code>ObjectResult</code>. The solution accepted there is to use <code>options.SuppressMapClientErrors = true;</code>, however I want to retain this mapping at the end of the pipeline. But if this option isn't set to true, I have no idea how to intercept the <code>Result</code> in the pipeline before this transformation.</p>
  <p><strong>So in my case, I want to do something with the result of the anti-forgery validation as mentioned in the linked post, but after that I want to retain the <code>ProblemDetails</code> transformation.</strong> But my question is titled generally, as it is about executing filters before the aforementioned client mapping filter.</p>
  <p>Through hacking I am able to achieve what I want. If we take a look at the <a href="https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Infrastructure/ClientErrorResultFilter.cs#L13" rel="nofollow noreferrer">source code</a>, we can see that the filter I want to precede has an order of <code>-2000</code>. So if I register my global filter like this <code>o.Filters.Add(typeof(MyResultFilter), -2001);</code>, then the filter shown <a href="https://stackoverflow.com/q/61074642/6389395">here</a> correctly executes before <code>ClientErrorResultFilter</code> and thus I can handle the result and retain the transformation after the handling. However I feel like this is just exploiting the open-source-ness of .Net 6 and of course as you can see it's an internal constant, so I have no guarantee the next patch doesn't change it and my code breaks. Surely there must be a proper way to order my filter to run before the api transform.</p>