Get explicit url entered in browser with asp.net core












0















I am working on an ASP.NET Core 2.1 application that uses ReactJS on the client and would like to log the urls entered explicitly in the browser for tracking purposes. For example, I would like to know when a user explicitly enters: http://myapp.com/; http://myapp.com/contact; http://myapp.com/help; etc... in the browser. I'm able to track when a user clicks on various links once they're already in http://myapp.com using Javascript, but it's when a user enters it directly in the browser (or clicks on a link from a google search) that I'm currently unable to track.



I've been looking at url middleware rewrite as well as trying to find a way to access the HttpContext from something like ConfigureServices in the Startup class, but I'm not able to figure this out.



Any help would be appreciated.










share|improve this question























  • Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    – B12Toaster
    Nov 21 '18 at 19:17


















0















I am working on an ASP.NET Core 2.1 application that uses ReactJS on the client and would like to log the urls entered explicitly in the browser for tracking purposes. For example, I would like to know when a user explicitly enters: http://myapp.com/; http://myapp.com/contact; http://myapp.com/help; etc... in the browser. I'm able to track when a user clicks on various links once they're already in http://myapp.com using Javascript, but it's when a user enters it directly in the browser (or clicks on a link from a google search) that I'm currently unable to track.



I've been looking at url middleware rewrite as well as trying to find a way to access the HttpContext from something like ConfigureServices in the Startup class, but I'm not able to figure this out.



Any help would be appreciated.










share|improve this question























  • Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    – B12Toaster
    Nov 21 '18 at 19:17
















0












0








0








I am working on an ASP.NET Core 2.1 application that uses ReactJS on the client and would like to log the urls entered explicitly in the browser for tracking purposes. For example, I would like to know when a user explicitly enters: http://myapp.com/; http://myapp.com/contact; http://myapp.com/help; etc... in the browser. I'm able to track when a user clicks on various links once they're already in http://myapp.com using Javascript, but it's when a user enters it directly in the browser (or clicks on a link from a google search) that I'm currently unable to track.



I've been looking at url middleware rewrite as well as trying to find a way to access the HttpContext from something like ConfigureServices in the Startup class, but I'm not able to figure this out.



Any help would be appreciated.










share|improve this question














I am working on an ASP.NET Core 2.1 application that uses ReactJS on the client and would like to log the urls entered explicitly in the browser for tracking purposes. For example, I would like to know when a user explicitly enters: http://myapp.com/; http://myapp.com/contact; http://myapp.com/help; etc... in the browser. I'm able to track when a user clicks on various links once they're already in http://myapp.com using Javascript, but it's when a user enters it directly in the browser (or clicks on a link from a google search) that I'm currently unable to track.



I've been looking at url middleware rewrite as well as trying to find a way to access the HttpContext from something like ConfigureServices in the Startup class, but I'm not able to figure this out.



Any help would be appreciated.







asp.net-core asp.net-core-2.1






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:03









Los MoralesLos Morales

479619




479619













  • Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    – B12Toaster
    Nov 21 '18 at 19:17





















  • Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    – B12Toaster
    Nov 21 '18 at 19:17



















Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

– B12Toaster
Nov 21 '18 at 19:17







Try using the IHttpContextAccessor: e.g. httpContextAccessor.HttpContext.Request.Host.Value; Register it via: services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

– B12Toaster
Nov 21 '18 at 19:17














2 Answers
2






active

oldest

votes


















3














Writing Middleware is fairly straight forward, adapting the example slightly from Microsoft Doc will log the URL.



app.Use(async (context, next) =>
{
var logger = new MyLogger();
var requestPath = context.Request.Path;
logger.Log(requestPath);
await next.Invoke();
});


However, I don't think it's possible to discern from the HttpContext whether the URL was entered in the browser address bar, versus any old GET request.






share|improve this answer
























  • Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

    – Los Morales
    Nov 22 '18 at 17:04



















2














You can check for Referer Headers. Asp.Net Core has an http extension library which has an extension method to get the typed headers.



Add this:



using Microsoft.AspNetCore.Http.Extensions;


Then access the Referer using the GetTypedHeaders() extension on a HttpContext, these are some of the properties:



httpContext.Request.GetTypedHeaders().Referer.AbsolutePath
httpContext.Request.GetTypedHeaders().Referer.AbsoluteUri
httpContext.Request.GetTypedHeaders().Referer.Authority
httpContext.Request.GetTypedHeaders().Referer.Host
httpContext.Request.GetTypedHeaders().Referer.PathAndQuery


Say our Refering Url is like:



http://localhost:4200/profile/users/1?x=1


The above properties will have these values:



/profile/users/1
http://localhost:4200/profile/users/1?x=1
localhost:4200
localhost
/profile/users/1?x=1





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%2f53418939%2fget-explicit-url-entered-in-browser-with-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









    3














    Writing Middleware is fairly straight forward, adapting the example slightly from Microsoft Doc will log the URL.



    app.Use(async (context, next) =>
    {
    var logger = new MyLogger();
    var requestPath = context.Request.Path;
    logger.Log(requestPath);
    await next.Invoke();
    });


    However, I don't think it's possible to discern from the HttpContext whether the URL was entered in the browser address bar, versus any old GET request.






    share|improve this answer
























    • Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

      – Los Morales
      Nov 22 '18 at 17:04
















    3














    Writing Middleware is fairly straight forward, adapting the example slightly from Microsoft Doc will log the URL.



    app.Use(async (context, next) =>
    {
    var logger = new MyLogger();
    var requestPath = context.Request.Path;
    logger.Log(requestPath);
    await next.Invoke();
    });


    However, I don't think it's possible to discern from the HttpContext whether the URL was entered in the browser address bar, versus any old GET request.






    share|improve this answer
























    • Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

      – Los Morales
      Nov 22 '18 at 17:04














    3












    3








    3







    Writing Middleware is fairly straight forward, adapting the example slightly from Microsoft Doc will log the URL.



    app.Use(async (context, next) =>
    {
    var logger = new MyLogger();
    var requestPath = context.Request.Path;
    logger.Log(requestPath);
    await next.Invoke();
    });


    However, I don't think it's possible to discern from the HttpContext whether the URL was entered in the browser address bar, versus any old GET request.






    share|improve this answer













    Writing Middleware is fairly straight forward, adapting the example slightly from Microsoft Doc will log the URL.



    app.Use(async (context, next) =>
    {
    var logger = new MyLogger();
    var requestPath = context.Request.Path;
    logger.Log(requestPath);
    await next.Invoke();
    });


    However, I don't think it's possible to discern from the HttpContext whether the URL was entered in the browser address bar, versus any old GET request.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 19:19









    Adam VincentAdam Vincent

    1,509527




    1,509527













    • Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

      – Los Morales
      Nov 22 '18 at 17:04



















    • Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

      – Los Morales
      Nov 22 '18 at 17:04

















    Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

    – Los Morales
    Nov 22 '18 at 17:04





    Like you mentioned, this grabs and logs every request. Will need to figure out a way to just get the browser url entered, or at least filter it out.

    – Los Morales
    Nov 22 '18 at 17:04













    2














    You can check for Referer Headers. Asp.Net Core has an http extension library which has an extension method to get the typed headers.



    Add this:



    using Microsoft.AspNetCore.Http.Extensions;


    Then access the Referer using the GetTypedHeaders() extension on a HttpContext, these are some of the properties:



    httpContext.Request.GetTypedHeaders().Referer.AbsolutePath
    httpContext.Request.GetTypedHeaders().Referer.AbsoluteUri
    httpContext.Request.GetTypedHeaders().Referer.Authority
    httpContext.Request.GetTypedHeaders().Referer.Host
    httpContext.Request.GetTypedHeaders().Referer.PathAndQuery


    Say our Refering Url is like:



    http://localhost:4200/profile/users/1?x=1


    The above properties will have these values:



    /profile/users/1
    http://localhost:4200/profile/users/1?x=1
    localhost:4200
    localhost
    /profile/users/1?x=1





    share|improve this answer




























      2














      You can check for Referer Headers. Asp.Net Core has an http extension library which has an extension method to get the typed headers.



      Add this:



      using Microsoft.AspNetCore.Http.Extensions;


      Then access the Referer using the GetTypedHeaders() extension on a HttpContext, these are some of the properties:



      httpContext.Request.GetTypedHeaders().Referer.AbsolutePath
      httpContext.Request.GetTypedHeaders().Referer.AbsoluteUri
      httpContext.Request.GetTypedHeaders().Referer.Authority
      httpContext.Request.GetTypedHeaders().Referer.Host
      httpContext.Request.GetTypedHeaders().Referer.PathAndQuery


      Say our Refering Url is like:



      http://localhost:4200/profile/users/1?x=1


      The above properties will have these values:



      /profile/users/1
      http://localhost:4200/profile/users/1?x=1
      localhost:4200
      localhost
      /profile/users/1?x=1





      share|improve this answer


























        2












        2








        2







        You can check for Referer Headers. Asp.Net Core has an http extension library which has an extension method to get the typed headers.



        Add this:



        using Microsoft.AspNetCore.Http.Extensions;


        Then access the Referer using the GetTypedHeaders() extension on a HttpContext, these are some of the properties:



        httpContext.Request.GetTypedHeaders().Referer.AbsolutePath
        httpContext.Request.GetTypedHeaders().Referer.AbsoluteUri
        httpContext.Request.GetTypedHeaders().Referer.Authority
        httpContext.Request.GetTypedHeaders().Referer.Host
        httpContext.Request.GetTypedHeaders().Referer.PathAndQuery


        Say our Refering Url is like:



        http://localhost:4200/profile/users/1?x=1


        The above properties will have these values:



        /profile/users/1
        http://localhost:4200/profile/users/1?x=1
        localhost:4200
        localhost
        /profile/users/1?x=1





        share|improve this answer













        You can check for Referer Headers. Asp.Net Core has an http extension library which has an extension method to get the typed headers.



        Add this:



        using Microsoft.AspNetCore.Http.Extensions;


        Then access the Referer using the GetTypedHeaders() extension on a HttpContext, these are some of the properties:



        httpContext.Request.GetTypedHeaders().Referer.AbsolutePath
        httpContext.Request.GetTypedHeaders().Referer.AbsoluteUri
        httpContext.Request.GetTypedHeaders().Referer.Authority
        httpContext.Request.GetTypedHeaders().Referer.Host
        httpContext.Request.GetTypedHeaders().Referer.PathAndQuery


        Say our Refering Url is like:



        http://localhost:4200/profile/users/1?x=1


        The above properties will have these values:



        /profile/users/1
        http://localhost:4200/profile/users/1?x=1
        localhost:4200
        localhost
        /profile/users/1?x=1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 6:10









        Tarik TutuncuTarik Tutuncu

        46329




        46329






























            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%2f53418939%2fget-explicit-url-entered-in-browser-with-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

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

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