I have a HttpGet endpoint that takes in an id
. I have figured out how to get the controller and action names but I am also wanting to get the parameters used in the endpoint because I want to be able to distinguish them from a normal default GET.
Here is the endpoint and example of what I am wanting:
// GET: api/PatientSearch/5
[HttpGet("{id}", Name = "Get")]
public string Get(int id)
{
_customLogger.LogInfoEvent(this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString());
return "value";
}
This currently works and logs the action as 'Get' But id' like it to log as 'Get {id}'
// GET: api/PatientSearch/5
[HttpGet("{id}", Name = "Get")]
public string Get(int id)
{
var parameters = MethodBase.GetCurrentMethod().GetParameters();
var paramNames = String.Join(", ", parameters.Select(p => p.Name)); // myString, myInt
return this.ControllerContext.RouteData.Values["action"] + "{"+ paramNames + "}";
}
Screenshots of test result:
public string Get(int id)
public string Get(int id, string str)
var q = ControllerContext.RouteData.Values["id"];
or
var q = id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.