Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Blazor client-side read hash parameters from url

Tags:

c#

blazor

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 ?;
}
}
like image 959
Igor Cova Avatar asked Nov 14 '18 19:11

Igor Cova


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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);
like image 170
enet Avatar answered Oct 18 '22 11:10

enet


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;
  }
}
like image 23
Igor Cova Avatar answered Oct 18 '22 13:10

Igor Cova