Reference general url parameter in AuthorizeAttribute

If my action has a path like /controller/action/{id}, I can get idin AuthorizeAttribute by doing httpContext.Request.RequestContext.RouteData.Values["id"].

Conversely, if it is something like /controller/action?id={id}, I can get it by doing httpContext.Request.QueryString["id"].

I will need another way if it forms the data from POST.

Is there a way to say: "Get what you specify in the parameter with the name" id ", regardless of how the route is specified?"

+2
source share
1 answer
var id = Request.RequestContext.RouteData.Values["id"] ?? Request.Params["id"] as string;

or if you want to assign GET and POST parameters in favor of route data:

var id = Request.Params["id"] ?? Request.RequestContext.RouteData.Values["id"] as string;
+6
source

All Articles