0

is there another way on getting the current url in mvc same in the code in window.location.href, so that i can manage the content?,

Problem

i have a url https://Mylink.com/Data?#address=ph that i want to get address value. my problem is, when i tried to get the url by doing Request in controller, only the https://Mylink.com/Data will get, querystring is also empty.

My Codes:

public string Data()
{
    var url = Request.Url;
    var addr = url.IndexOf('#') > -1 ? url.Substring(url.IndexOf('#'),url.Length): "";
    return addr;
}

any suggestion will be accepted, thank again in advance

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
vaj90
  • 1,803
  • 3
  • 17
  • 17

1 Answers1

2

The problem is that your querystring is empty. The # and everything after it is known as the fragment and is a clientside only thing. In general this is not sent to the server.

If you need information from here on the server then chances are it should be in the querystring instead of the fragment.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • And then you can use `Request.Url.Query` – Colin Bacon Oct 23 '14 at 12:35
  • yup is # is the problem, but is there another way on getting the whole url in controller so that i'll split the # character? – vaj90 Oct 23 '14 at 12:40
  • 1
    @A.J: See the question that this has been marked a duplicate of for some ways round it. Essentially it boils down to having script get the fragment and putting it somewhere you can access it. If this is a form post then you could put it in a hidden field. If its a GET request then your only option is to put it in the querystring. – Chris Oct 23 '14 at 12:44
  • thank again sir, i'll used window.location to solved this. – vaj90 Oct 23 '14 at 12:49