For creating a local diskcache of resized images, I am trying to figure out how I can create a URL rewrite during runtime.
Something like this:
[Route("(images/{id}.jpg")]
public IActionResult ResizeImage(int id, int height, int width)
{
string webRoot = _env.ContentRootPath;
var file = System.IO.Path.Combine(webRoot, $"resizedimage{id}.jpg");
//Pseudocode:
UrlRewriter.DoMagic($"images/{id}.jpg?height={height}&width={width}", "/resizedimage{id}.jpg")
return File(file, "image/jpeg");
}
A client requests /images/123.jpg?height=100&width=100 ...
I can create a static url rewrite, that rewrites /images/123.jpg?height=100&width=100 to /images/resizedimage.jpg (resized image on disk), bypassing the action method (presumably)..
How can I do the same thing on the fly, with something like the above pseudocode?
Note:
I do not care about the first request, which WILL hit the actionmethod and be served the image via a fileresult (as shown above), only subsequent requests to the same url.
I am aware of methods to create dynamic url rewrites at startup, but not runtime (which is what I am asking about)
Yes, I could just return a redirect to the image file, which is also pretty efficient - but it is still two synchrounous requests from the client.
ASP.Net Core 2+ required