One of the things I do a lot of is web services, REST APIs. I recently created a versioned API that allows us to release new versions without breaking existing clients (of which there could be many).
The Microsoft versioning stuff is good, very easy to use but we noticed that it treats these the same:
https://localhost:7276/api/v1/DoctoralRecord/EnrollStudent
https://localhost:7276/api/v1.0/DoctoralRecord/EnrollStudent
There was no simple way to tell the versioning libraries to enforce this format. So I asked copilot how can I intercept the URL and "manually" inspect it and reject any request that does not specify version number as digits.digits.
I've done this kind of stuff before, but it always involves digging into docs and scouring web articles and blogs to remind me of the details, but not with Copilot it generated the middleware class for me:
I just dumped it into the project, tweaked the setup to use it and tested and it worked fine (it isn't complex).
Saved me at least 30 minutes because intercept middleware isn't something I write regular and I don't recall details like this very much.
Saving 30 minutes three or four times a day adds up to about 10 man hours saved per week.
The Microsoft versioning stuff is good, very easy to use but we noticed that it treats these the same:
https://localhost:7276/api/v1/DoctoralRecord/EnrollStudent
https://localhost:7276/api/v1.0/DoctoralRecord/EnrollStudent
There was no simple way to tell the versioning libraries to enforce this format. So I asked copilot how can I intercept the URL and "manually" inspect it and reject any request that does not specify version number as digits.digits.
I've done this kind of stuff before, but it always involves digging into docs and scouring web articles and blogs to remind me of the details, but not with Copilot it generated the middleware class for me:
C#:
public class ApiVersionValidationMiddleware
{
private readonly RequestDelegate _next;
private static readonly Regex _versionRegex = new Regex(@"^v\d+\.\d+$", RegexOptions.Compiled);
public ApiVersionValidationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var path = context.Request.Path.Value;
// Extract the version segment (e.g., /api/v1.5/controller → v1.5)
var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
var versionSegment = segments.FirstOrDefault(s => s.StartsWith("v"));
if (versionSegment != null && !_versionRegex.IsMatch(versionSegment))
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid API version format. You must specify vX.Y (e.g., v1.5)");
return;
}
await _next(context);
}
}
Saved me at least 30 minutes because intercept middleware isn't something I write regular and I don't recall details like this very much.
Saving 30 minutes three or four times a day adds up to about 10 man hours saved per week.
Last edited: