5

I want to get the current URL into a string that I can use for e.g. share functionality later.

String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;

I get the error:

The name HttpContext does not exist in the current context.

Is there another way?

Satpal
  • 129,808
  • 12
  • 152
  • 166
Mr.Anderson
  • 51
  • 1
  • 3

2 Answers2

10

In case you are using MVC 3 or higher: Here is a related questions.

In case you are using ASP .Net MVC Core 1.0 you should use this code:

Context.Request.Path
Community
  • 1
  • 1
Hello It's me
  • 528
  • 10
  • 24
0

I believe that you can do Request.RawUrl and it should return the entire current page URL.

So you would do:

@{
  string strPathAndQuery = Request.RawUrl;    
}
amoyer
  • 413
  • 4
  • 13
  • I get the same error msg. I believe it is something wrong with the dependencies, since I need to use system.web.mvc..But when I try to add that NuGet I get errors in my References. – Mr.Anderson May 25 '16 at 13:14
  • What kind of errors? What does your view look like? – amoyer May 25 '16 at 13:33
  • You can also look at this question to see if it helps: http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser?rq=1 – amoyer May 25 '16 at 13:34
  • I'll get back too you with the errors. I tired the string url = window.location.href; but getting the same The name 'window' does not exist in the current context. – Mr.Anderson May 25 '16 at 14:56