How to get URL template path in ASP.NET Core?












0















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")].










share|improve this question























  • 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
















0















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")].










share|improve this question























  • 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














0












0








0








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")].










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















1














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 :)






share|improve this answer































    0














    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.






    share|improve this answer


























      Your Answer






      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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 :)






      share|improve this answer




























        1














        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 :)






        share|improve this answer


























          1












          1








          1







          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 :)






          share|improve this answer













          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 :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 22:23









          FarshanFarshan

          16119




          16119

























              0














              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.






              share|improve this answer






























                0














                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.






                share|improve this answer




























                  0












                  0








                  0







                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 20:52

























                  answered Jan 2 at 20:44









                  Daniel A. WhiteDaniel A. White

                  150k37297377




                  150k37297377






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory