3

I want to get the current page absolute url in sharepoint.. I'm using:

this.Page.Request.Url.ToString()

But for:

http://e-albania.tirana.bz:6002/sites/ealbania02/_layouts/IISNewsBox/news_view.aspx

it returns

http://e-albania.tirana.bz:6002/_layouts/IISNewsBox/news_view.aspx

Maybe cause layouts folder is the same for all the web applications.. How do I get the correct url?

1
  • 1
    Have you tried Page.Request.RawUrl?
    – user2536
    Sep 17, 2012 at 8:34

3 Answers 3

7

Code below should do the trick

Page.Request.RawUrl
1
  • Great! Though it only gives you the relative URL without the http://domain/ part
    – Dennis G
    Sep 27, 2012 at 16:36
3

HttpContext.Current.Request.Url

or

https://stackoverflow.com/questions/9701309/get-app-relative-url-from-request-url-absolutepath

or

SPSite.MakeFullUrl()

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.makefullurl.aspx

an example is:

using(SPSite site = new SPSite("http://e-albania.tirana.bz"))
{     using(SPWeb web = site.OpenWeb())
    {
      string absoluteURL = site.MakeFullUrl(this.Page.Request.Url.ToString());
    }
}

hope it helps :)

3
  • The time-battle forced you to update the answer? :) In any case very nice. +1
    – eirikb
    Sep 17, 2012 at 9:03
  • well what can i say it a race against the time and I wanted to make sure he had a selection to choose from instead of HttpContext.Current.Request.Url lol.
    – Ali Jafer
    Sep 17, 2012 at 9:07
  • Thanx but I don't know why HttpContext.Current.Request.Url doesn't work, instead Page.Request.RawUrl did it perfectly.
    – MIlena
    Sep 17, 2012 at 9:21
0

Try to use System.Web.HttpContext.Current.Request.Url

It should give you the current pages url.

1
  • it doesn't give you the url including subsites - maybe sharepoints custom iis pipeline at work
    – Dennis G
    Sep 27, 2012 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.