How can I get fragment value from server side?

My url is:

http://localhost:4567/Test/Callback#state=test&access_token=....

But when calling Request.Url.ToString(); it just outputs

http://localhost:4567/Test/Callback

How can I get the full url send to the server?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can’t.

There is a big difference between hash (#) and query string (?). The query string is send to the server, the hash isn’t.

So the url send to the server is: http://localhost:4567/Test/Callback.

The only option you have to get the ‘hash’ to the server is by using a query string:

http://localhost:4567/Test/Callback?state=test&access_token=...

Method 2

var uri = new Uri("http://localhost:4567/Test/Callback#state=test&access_token=....");

// Contains the query
uri.Fragment

Results in:

#state=test&access_token=....

Edit:

To get the current url of website use:

Request.Url.AbsoluteUri;

In the Request.Url is all the information of the current page and in Request.UrlReffer everything from the previous page.

Note: Request.UrlReferrer is null when there is no previous request (from your website)

Method 3

var <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0772756b3a47">[email protected]</a>"http://localhost:4567/Test/Callback#state=test";
var uri = new Uri(url);
var result = uri.Fragment;

Method 4

Others have already posted answers to your specific problem.

But it seems like you are developing an ASP.NET website so you should consider using the standard ? instead of # to prefix your query string.

This will allow you to use built-in methods and properties for processing the query string and avoid custom error-prone string processing:

string queryString = Request.Url.Query; // gives you "state=test&access_token=...."

or access it as a NameValueCollection:

string state = Request.QueryString["state"]; // gives you "test"

Method 5

you can use javascript to get the full link then pass it to the code behind

<script language="javascript" type="text/javascript">

    function JavaScriptFunction() {
        document.getElementById('<%= hdnResultValue.ClientID %>').value = document.URL;
    }

</script>

<asp:HiddenField ID="hdnResultValue" Value="0" runat="server" />
        <asp:Button ID="Button_Get" runat="server" Text="run" OnClick="Button_Get_Click" OnClientClick="JavaScriptFunction();" />

then from the code behind get the value of hiddenfield which contains the current full URL

protected void Button_Get_Click(object sender, EventArgs e)
{
   string fullURL = hdnResultValue.Value;
   string URl = fullURL .Substring(fullURL .IndexOf('#') + 1);
}

good luck


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x