RewriteMiddleware sets endpoint in pipeline before routing middleware
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
I'm attempting to use the RewriteMiddleware to (in part) select alternate paths for static files. If I use the [UseRewriter](https://github.com/dotnet/aspnetcore/blob/14c39984660a8cefb09a8d77331b47ffc48d7a22/src/Middleware/Rewrite/src/RewriteBuilderExtensions.cs#L58) extension to register the middleware in the pipeline, endpoint evaluation/selection occurs sooner than expected on rewritten requests, which causes StaticFilesMiddleware to be [bypassed](https://github.com/dotnet/aspnetcore/blob/25ffef8fe5ad0ed67e0ad33b02379c2d3c1a890f/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs#L75) when an alternate endpoint is found. I'm using YARP to route all unserviceable requests to another application, so the YARP endpoint will always be selected in my case.
### Expected Behavior
I would expect the rewrite middleware to simply rewrite the request path and let the pipeline continue as configured.
### Steps To Reproduce
Example code here: https://github.com/bgulrich/rewrite-bug
I took the template ASP.NET Core 6 WebApplication and moved `wwwroot/favicon.ico` -> `wwwroot/test/favicon.ico` and configured the rewrite middlware to point `/favicon.ico` request to this new location. I also added the `TestController` with an endpoint on the `/test/favicon.ico` route. My expectation is that this endpoint should not be reachable because it should be serviced by the static files middleware that appears earlier in the pipeline, but issuing a request to `/favicon.ico` hits this endpoint instead because the RewriteMiddleware is setting the endpoint (checked with breakpoint in dummy middleware between rewrite and static files), causing StaticFilesMiddleware to bypass.
### Exceptions (if any)
_No response_
### .NET Version
6.0.400
### Anything else?
My current workaround is to register my RewriteOptions with the service provider and to register the RewriteMiddleware manually with `app.Use()` to avoid the UseRewrite extension that tweaks the pipline.
Contributor guide
Assessment
This issue has not been assessed yet.