Rewrite the Route after UseRouting was called inside a middle-ware #22699
Replies: 2 comments 2 replies
-
@TwentyFourMinutes thanks for contacting us. Can you describe what you are trying to accomplish so that we can provide guidance on how to best approach it? It is not clear why you are trying to change the request path after routing has run and what you expect do accomplish in doing so. |
Beta Was this translation helpful? Give feedback.
-
Sure thing, basically I want to implement a rate-limit system on a per user basis. I do want to show a View, which tells you that you have exceeded the limit, instead of the actual content. I don't want to redirect the user, which would also cause the url in the browser to change. Additionally the whole middle-ware pipeline would be executed again which IMO is somewhat useless. |
Beta Was this translation helpful? Give feedback.
-
Just to clarify, every endpoint needs authorization in my ASP.Net Core 3.1 MVC website, except the SignIn method obviously. Inside a middle-ware I am writing I need to rewrite the route, since I do not want to re-execute the whole pipeline again and don't want to redirect the user to some
/Error/404
view. Additionally I do need access to theClaimsPrinciple
inside the middle-ware.Just note I am currently using Cookie Authentication.
There are two approaches I could think of, where the first one would look something like this:
However, even with my best efforts I could't get it to change the selected Endpoint by
UseRouting
.These are all the things I tried:
HttpContext.Request.Path
(Directly assigning it something like/Some/Path
)IRouteValuesFeature
(Change thearea
,controller
andaction
strings)IEndpointFeature
(Assign itnull
)Sadly none of these things worked, they either started to produce 404 or just executed the first-matched route by
UseRouting
.The other method I could think of was moving the Authentication, Authorization and MyMiddleware
middle-wares above the
UseRouting
.Now I would be able to rewrite the route with setting the
HttpContext.Request.Path
, however as the doc explicitly quotes, you should put the auth stuff after theUseRouting
.I would assume that overall to make this work the second idea would be more possible than the first one. So would there be any way to achieve any of those methods?
Beta Was this translation helpful? Give feedback.
All reactions