How to get URL template path in ASP.NET Core?
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")]
.
c# asp.net-core routing asp.net-core-webapi
add a comment |
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")]
.
c# asp.net-core routing asp.net-core-webapi
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17
add a comment |
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")]
.
c# asp.net-core routing asp.net-core-webapi
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")]
.
c# asp.net-core routing asp.net-core-webapi
c# asp.net-core routing asp.net-core-webapi
asked Jan 2 at 20:31


Dmytro ZhluktenkoDmytro Zhluktenko
757
757
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17
add a comment |
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17
add a comment |
2 Answers
2
active
oldest
votes
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 :)
add a comment |
You can get part of it from the context.ActionDescriptor.AttributeRouteInfo
. I'm not 100% sure if that would be complete or just a partial piece of it.
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012776%2fhow-to-get-url-template-path-in-asp-net-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 :)
add a comment |
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 :)
add a comment |
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 :)
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 :)
answered Jan 2 at 22:23
FarshanFarshan
16119
16119
add a comment |
add a comment |
You can get part of it from the context.ActionDescriptor.AttributeRouteInfo
. I'm not 100% sure if that would be complete or just a partial piece of it.
add a comment |
You can get part of it from the context.ActionDescriptor.AttributeRouteInfo
. I'm not 100% sure if that would be complete or just a partial piece of it.
add a comment |
You can get part of it from the context.ActionDescriptor.AttributeRouteInfo
. I'm not 100% sure if that would be complete or just a partial piece of it.
You can get part of it from the context.ActionDescriptor.AttributeRouteInfo
. I'm not 100% sure if that would be complete or just a partial piece of it.
edited Jan 2 at 20:52
answered Jan 2 at 20:44
Daniel A. WhiteDaniel A. White
150k37297377
150k37297377
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012776%2fhow-to-get-url-template-path-in-asp-net-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
just wondering why you would need that?
– Daniel A. White
Jan 2 at 20:35
Suggest you provide a minimal complete example. As asked it's not clear what you want.
– No Refunds No Returns
Jan 2 at 20:40
@DanielA.White Action filter to collect Prometheus metrics on a controller. I need this template path for a label on a metric.
– Dmytro Zhluktenko
Jan 3 at 12:17