6

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
  • 132,252
  • 13
  • 159
  • 168
Mr.Anderson
  • 61
  • 1
  • 1
  • 3

2 Answers2

11

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
  • 538
  • 1
  • 11
  • 24
1

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;    
}
moyeradon
  • 443
  • 5
  • 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? – moyeradon 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 – moyeradon 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