[英]How to get URL template path in ASP.NET Core?
我正在使用ActionFilter,需要获取URL模板路径,例如/api/v{version}/controllerName/actionName/{parameter}
。 但是,该解决方案需要通用,因此它支持多种URL模式,例如api/v{version}/controllerName/actionName/{parameter}/otherActionName/{otherParameter}
。
ASP.NET Core 2.2最新稳定。 我所拥有的是ActionExecutedContext
,它也包含HttpContext
。 但是,我不确定此HttpContext
的内容,因为它包含响应的一些默认值。
private PathString GetUrlTemplatePath(ActionExecutedContext context)
{
// TODO:
return context.HttpContext.Request.Path;
}
实际结果: /api/v1/Location/addresses/999999A1-458A-42D0-80AA-D08A3DAD8199
。
预期结果: /api/v{version}/location/addresses/{externalId}
,其中externalId
是由属性[HttpGet("addresses/{externalId}", Name = "GetAddress")]
描述的参数[HttpGet("addresses/{externalId}", Name = "GetAddress")]
。
您可以从ResourceFilter的ActionExecutedContext
获取模板路径。 如果您的问题需要QueryString
或在Dictionary<string, object>
类型的上下文中有ActionArguments
,其中包含与请求一起传递的所有参数。
//template
string template = context.ActionDescriptor.AttributeRouteInfo.Template;;
//arguments
IDictionary<string, object> arguments = context.ActionArguments;
//query string
string queryString= context.HttpContext.Request.QueryString.Value;
希望这可以帮助 :)
您可以从context.ActionDescriptor.AttributeRouteInfo
获得它的一部分。 我不确定这是完整的还是部分的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.