I'm working on ActionFilter and need to get URL template path like /api/v{version}/controllerName/actionName/{parameter}. However, the solution needs to be generic so it supports multiple URL patterns like api/v{version}/controllerName/actionName/{parameter}/otherActionName/{otherParameter}.
ASP.NET Core 2.2 latest stable. What I have is ActionExecutedContext which contains HttpContext as well. However, I am not sure about the content of this HttpContext since it contains some default values for the response.
private PathString GetUrlTemplatePath(ActionExecutedContext context)
{
// TODO:
return context.HttpContext.Request.Path;
}
Actual result: /api/v1/Location/addresses/999999A1-458A-42D0-80AA-D08A3DAD8199.
Expected result: /api/v{version}/location/addresses/{externalId} where externalId is a name of parameter described by an attribute [HttpGet("addresses/{externalId}", Name = "GetAddress")].
You can get the template path from your ActionExecutedContext from your ResourceFilter. if you need QueryString for your problem or there is ActionArguments in the context of type Dictionary<string, object>which contains all the parameters passed with the request.
//template
string template = context.ActionDescriptor.AttributeRouteInfo.Template;;
//arguments
IDictionary<string, object> arguments = context.ActionArguments;
//query string
string queryString= context.HttpContext.Request.QueryString.Value;
Hope this helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With