How to get RouteData inside a .net core middleware?
Asked Answered
A

1

6

I am working on a .net core app. I am trying to access route data in a middleware but it always returns null.

enter image description here

Here is the controller base route

[Route("User/{UserId}/Quote")]
    public class QuoteController : ControllerBase

My objective is to try to get UserId inside middleware which I am using it for authentication somewhere else. My question is why the GetRouteData() call always return null?

Applejack answered 13/7, 2018 at 0:40 Comment(3)
If you do this before UseMvc then I don't think you will have access to the routing.Waitabit
what is strange is that if I put code after UseMvc(), it does not even get to this line.Applejack
I have the exact same experience as @ApplejackTendency
H
4

Route data is set by the MVC middleware which comes into picture further down the stream. You have to move the context.GetRouteData() call to the line after the await next call to access the route data.

See the picture at https://mcmap.net/q/134949/-asp-net-core-middleware-vs-filters for reference.

what is strange is that if I put code after UseMvc(), it does not even get to this line

Once MVC middleware handles the request it sends the response back to the client and doesnt handle the control to middleware next in the pipeline.

Heredity answered 13/7, 2018 at 5:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.