C# Blazor client-side read hash parameters from url










2















I have a project in Blazor



And on the client-side, I want to read hash parameters



I know how to do it in JavaScript - but my question is how to do it in c# client-side in Blazor project



For example, I have an URL http://localhost:5060/#token=12345678



How to take token?



my code in index.cshtml



@page "/"
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>Hello, world!</h1>

url is @Url

@functions
protected override void OnInit()
Url = GetUrl();


public string Url get; set;

public string GetUrl()
return ?;











share|improve this question



















  • 2





    @DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

    – Nick Polyderopoulos
    Nov 14 '18 at 19:17






  • 1





    Perhaps this might help: stackoverflow.com/questions/50102726/…

    – elgonzo
    Nov 14 '18 at 19:22











  • @elgonzo yes it works well. Thank you =)

    – Igor Cova
    Nov 14 '18 at 19:24











  • By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

    – elgonzo
    Nov 14 '18 at 19:26












  • Possible duplicate of Get current Url in a Blazor component

    – mjwills
    Nov 14 '18 at 20:37















2















I have a project in Blazor



And on the client-side, I want to read hash parameters



I know how to do it in JavaScript - but my question is how to do it in c# client-side in Blazor project



For example, I have an URL http://localhost:5060/#token=12345678



How to take token?



my code in index.cshtml



@page "/"
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>Hello, world!</h1>

url is @Url

@functions
protected override void OnInit()
Url = GetUrl();


public string Url get; set;

public string GetUrl()
return ?;











share|improve this question



















  • 2





    @DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

    – Nick Polyderopoulos
    Nov 14 '18 at 19:17






  • 1





    Perhaps this might help: stackoverflow.com/questions/50102726/…

    – elgonzo
    Nov 14 '18 at 19:22











  • @elgonzo yes it works well. Thank you =)

    – Igor Cova
    Nov 14 '18 at 19:24











  • By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

    – elgonzo
    Nov 14 '18 at 19:26












  • Possible duplicate of Get current Url in a Blazor component

    – mjwills
    Nov 14 '18 at 20:37













2












2








2








I have a project in Blazor



And on the client-side, I want to read hash parameters



I know how to do it in JavaScript - but my question is how to do it in c# client-side in Blazor project



For example, I have an URL http://localhost:5060/#token=12345678



How to take token?



my code in index.cshtml



@page "/"
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>Hello, world!</h1>

url is @Url

@functions
protected override void OnInit()
Url = GetUrl();


public string Url get; set;

public string GetUrl()
return ?;











share|improve this question
















I have a project in Blazor



And on the client-side, I want to read hash parameters



I know how to do it in JavaScript - but my question is how to do it in c# client-side in Blazor project



For example, I have an URL http://localhost:5060/#token=12345678



How to take token?



my code in index.cshtml



@page "/"
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>Hello, world!</h1>

url is @Url

@functions
protected override void OnInit()
Url = GetUrl();


public string Url get; set;

public string GetUrl()
return ?;








c# blazor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 6:16







Igor Cova

















asked Nov 14 '18 at 19:05









Igor CovaIgor Cova

632623




632623







  • 2





    @DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

    – Nick Polyderopoulos
    Nov 14 '18 at 19:17






  • 1





    Perhaps this might help: stackoverflow.com/questions/50102726/…

    – elgonzo
    Nov 14 '18 at 19:22











  • @elgonzo yes it works well. Thank you =)

    – Igor Cova
    Nov 14 '18 at 19:24











  • By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

    – elgonzo
    Nov 14 '18 at 19:26












  • Possible duplicate of Get current Url in a Blazor component

    – mjwills
    Nov 14 '18 at 20:37












  • 2





    @DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

    – Nick Polyderopoulos
    Nov 14 '18 at 19:17






  • 1





    Perhaps this might help: stackoverflow.com/questions/50102726/…

    – elgonzo
    Nov 14 '18 at 19:22











  • @elgonzo yes it works well. Thank you =)

    – Igor Cova
    Nov 14 '18 at 19:24











  • By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

    – elgonzo
    Nov 14 '18 at 19:26












  • Possible duplicate of Get current Url in a Blazor component

    – mjwills
    Nov 14 '18 at 20:37







2




2





@DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

– Nick Polyderopoulos
Nov 14 '18 at 19:17





@DawidFerenczy Blazor is a experimental C# framework for Client Side scripting. using WebAssembly blazor.net

– Nick Polyderopoulos
Nov 14 '18 at 19:17




1




1





Perhaps this might help: stackoverflow.com/questions/50102726/…

– elgonzo
Nov 14 '18 at 19:22





Perhaps this might help: stackoverflow.com/questions/50102726/…

– elgonzo
Nov 14 '18 at 19:22













@elgonzo yes it works well. Thank you =)

– Igor Cova
Nov 14 '18 at 19:24





@elgonzo yes it works well. Thank you =)

– Igor Cova
Nov 14 '18 at 19:24













By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

– elgonzo
Nov 14 '18 at 19:26






By the way, perhaps you could change your URL to use "route parameters". This could perhaps make it easier to map the token to a component property (see here: codedaze.io/blazor-bites-routing). Although, i have to admit i haven't done anything like that myself, so i might talk complete bollocks here ;-)

– elgonzo
Nov 14 '18 at 19:26














Possible duplicate of Get current Url in a Blazor component

– mjwills
Nov 14 '18 at 20:37





Possible duplicate of Get current Url in a Blazor component

– mjwills
Nov 14 '18 at 20:37












2 Answers
2






active

oldest

votes


















3














If you can do it in JavaScript, then use JavaScript Interop:
1. Define a JavaScript function which extract the token.
2. Define a C# method which call the function



But it would be still better to do that with Blazor which itself use JavaScript...
What you need is to look at the methods defined in Microsoft.AspNetCore.Blazor.Services.UriHelperBase and/or
Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper



Hope this helps...



Note: the <base> element is set in the Index.Html file located in the
wwwroot folder.




"The HTML <base> element specifies the base URL to use for all
relative URLs contained within a document. There can be only one
element in a document.



The base URL of a document can be queried from a script using
document.baseURI."




Try this:



var absoluteUrl = UriHelper.GetAbsoluteUri();
var token = absoluteUrl.Substring(absoluteUrl.IndexOf("=") + 1);





share|improve this answer
































    0














    For reading hash parameters in C# Blazor without JavaScript or other client-side solutions I need to change function, like in the code below:



    @functions 
    private string url = string.Empty;

    protected override void OnInit()
    string url = UriHelper.GetAbsoluteUri();
    string parameters = url .Replace(UriHelper.GetBaseUri(), "").Replace("#", "").Split('&');

    string token = string.Empty;

    foreach (string prm in parameters)
    if (prm.IndexOf("token=") >= 0)
    token = prm.Replace("token=", "");



    UriHelper.OnLocationChanged += OnLocationChanged;


    private void OnLocationChanged(object sender, string newUriAbsolute)
    url = newUriAbsolute;


    public void Dispose()
    UriHelper.OnLocationChanged -= OnLocationChanged;







    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%2f53307138%2fc-sharp-blazor-client-side-read-hash-parameters-from-url%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














      If you can do it in JavaScript, then use JavaScript Interop:
      1. Define a JavaScript function which extract the token.
      2. Define a C# method which call the function



      But it would be still better to do that with Blazor which itself use JavaScript...
      What you need is to look at the methods defined in Microsoft.AspNetCore.Blazor.Services.UriHelperBase and/or
      Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper



      Hope this helps...



      Note: the <base> element is set in the Index.Html file located in the
      wwwroot folder.




      "The HTML <base> element specifies the base URL to use for all
      relative URLs contained within a document. There can be only one
      element in a document.



      The base URL of a document can be queried from a script using
      document.baseURI."




      Try this:



      var absoluteUrl = UriHelper.GetAbsoluteUri();
      var token = absoluteUrl.Substring(absoluteUrl.IndexOf("=") + 1);





      share|improve this answer





























        3














        If you can do it in JavaScript, then use JavaScript Interop:
        1. Define a JavaScript function which extract the token.
        2. Define a C# method which call the function



        But it would be still better to do that with Blazor which itself use JavaScript...
        What you need is to look at the methods defined in Microsoft.AspNetCore.Blazor.Services.UriHelperBase and/or
        Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper



        Hope this helps...



        Note: the <base> element is set in the Index.Html file located in the
        wwwroot folder.




        "The HTML <base> element specifies the base URL to use for all
        relative URLs contained within a document. There can be only one
        element in a document.



        The base URL of a document can be queried from a script using
        document.baseURI."




        Try this:



        var absoluteUrl = UriHelper.GetAbsoluteUri();
        var token = absoluteUrl.Substring(absoluteUrl.IndexOf("=") + 1);





        share|improve this answer



























          3












          3








          3







          If you can do it in JavaScript, then use JavaScript Interop:
          1. Define a JavaScript function which extract the token.
          2. Define a C# method which call the function



          But it would be still better to do that with Blazor which itself use JavaScript...
          What you need is to look at the methods defined in Microsoft.AspNetCore.Blazor.Services.UriHelperBase and/or
          Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper



          Hope this helps...



          Note: the <base> element is set in the Index.Html file located in the
          wwwroot folder.




          "The HTML <base> element specifies the base URL to use for all
          relative URLs contained within a document. There can be only one
          element in a document.



          The base URL of a document can be queried from a script using
          document.baseURI."




          Try this:



          var absoluteUrl = UriHelper.GetAbsoluteUri();
          var token = absoluteUrl.Substring(absoluteUrl.IndexOf("=") + 1);





          share|improve this answer















          If you can do it in JavaScript, then use JavaScript Interop:
          1. Define a JavaScript function which extract the token.
          2. Define a C# method which call the function



          But it would be still better to do that with Blazor which itself use JavaScript...
          What you need is to look at the methods defined in Microsoft.AspNetCore.Blazor.Services.UriHelperBase and/or
          Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper



          Hope this helps...



          Note: the <base> element is set in the Index.Html file located in the
          wwwroot folder.




          "The HTML <base> element specifies the base URL to use for all
          relative URLs contained within a document. There can be only one
          element in a document.



          The base URL of a document can be queried from a script using
          document.baseURI."




          Try this:



          var absoluteUrl = UriHelper.GetAbsoluteUri();
          var token = absoluteUrl.Substring(absoluteUrl.IndexOf("=") + 1);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 22:10

























          answered Nov 14 '18 at 20:37









          IssacIssac

          1,7801312




          1,7801312























              0














              For reading hash parameters in C# Blazor without JavaScript or other client-side solutions I need to change function, like in the code below:



              @functions 
              private string url = string.Empty;

              protected override void OnInit()
              string url = UriHelper.GetAbsoluteUri();
              string parameters = url .Replace(UriHelper.GetBaseUri(), "").Replace("#", "").Split('&');

              string token = string.Empty;

              foreach (string prm in parameters)
              if (prm.IndexOf("token=") >= 0)
              token = prm.Replace("token=", "");



              UriHelper.OnLocationChanged += OnLocationChanged;


              private void OnLocationChanged(object sender, string newUriAbsolute)
              url = newUriAbsolute;


              public void Dispose()
              UriHelper.OnLocationChanged -= OnLocationChanged;







              share|improve this answer



























                0














                For reading hash parameters in C# Blazor without JavaScript or other client-side solutions I need to change function, like in the code below:



                @functions 
                private string url = string.Empty;

                protected override void OnInit()
                string url = UriHelper.GetAbsoluteUri();
                string parameters = url .Replace(UriHelper.GetBaseUri(), "").Replace("#", "").Split('&');

                string token = string.Empty;

                foreach (string prm in parameters)
                if (prm.IndexOf("token=") >= 0)
                token = prm.Replace("token=", "");



                UriHelper.OnLocationChanged += OnLocationChanged;


                private void OnLocationChanged(object sender, string newUriAbsolute)
                url = newUriAbsolute;


                public void Dispose()
                UriHelper.OnLocationChanged -= OnLocationChanged;







                share|improve this answer

























                  0












                  0








                  0







                  For reading hash parameters in C# Blazor without JavaScript or other client-side solutions I need to change function, like in the code below:



                  @functions 
                  private string url = string.Empty;

                  protected override void OnInit()
                  string url = UriHelper.GetAbsoluteUri();
                  string parameters = url .Replace(UriHelper.GetBaseUri(), "").Replace("#", "").Split('&');

                  string token = string.Empty;

                  foreach (string prm in parameters)
                  if (prm.IndexOf("token=") >= 0)
                  token = prm.Replace("token=", "");



                  UriHelper.OnLocationChanged += OnLocationChanged;


                  private void OnLocationChanged(object sender, string newUriAbsolute)
                  url = newUriAbsolute;


                  public void Dispose()
                  UriHelper.OnLocationChanged -= OnLocationChanged;







                  share|improve this answer













                  For reading hash parameters in C# Blazor without JavaScript or other client-side solutions I need to change function, like in the code below:



                  @functions 
                  private string url = string.Empty;

                  protected override void OnInit()
                  string url = UriHelper.GetAbsoluteUri();
                  string parameters = url .Replace(UriHelper.GetBaseUri(), "").Replace("#", "").Split('&');

                  string token = string.Empty;

                  foreach (string prm in parameters)
                  if (prm.IndexOf("token=") >= 0)
                  token = prm.Replace("token=", "");



                  UriHelper.OnLocationChanged += OnLocationChanged;


                  private void OnLocationChanged(object sender, string newUriAbsolute)
                  url = newUriAbsolute;


                  public void Dispose()
                  UriHelper.OnLocationChanged -= OnLocationChanged;








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 7:53









                  Igor CovaIgor Cova

                  632623




                  632623



























                      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%2f53307138%2fc-sharp-blazor-client-side-read-hash-parameters-from-url%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto