Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference generic url parameter in AuthorizeAttribute

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

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

I'll need yet another way if it's form data from a POST.

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

like image 732
Xodarap Avatar asked Feb 24 '23 21:02

Xodarap


1 Answers

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

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

var id = Request.Params["id"] ?? Request.RequestContext.RouteData.Values["id"] as string;
like image 181
Darin Dimitrov Avatar answered Mar 08 '23 18:03

Darin Dimitrov