How do I get the full url of the page I am on in C#
Here is a list I normally refer to for this type of information:
Request.ApplicationPath : /virtual_dir
Request.CurrentExecutionFilePath : /virtual_dir/webapp/page.aspx
Request.FilePath : /virtual_dir/webapp/page.aspx
Request.Path : /virtual_dir/webapp/page.aspx
Request.PhysicalApplicationPath : d:\Inetpub\wwwroot\virtual_dir\
Request.QueryString : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.AbsolutePath : /virtual_dir/webapp/page.aspx
Request.Url.AbsoluteUri : http://localhost:2000/virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Host : localhost
Request.Url.Authority : localhost:80
Request.Url.LocalPath : /virtual_dir/webapp/page.aspx
Request.Url.PathAndQuery : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Port : 80
Request.Url.Query : ?q=qvalue
Request.Url.Scheme : http
Request.Url.Segments : /
virtual_dir/
webapp/
page.aspx
Hopefully you will find this useful!
I usually use Request.Url.ToString()
to get the full url (including querystring), no concatenation required.
Request.Url.AbsoluteUri
This property does everything you need, all in one succinct call.
For ASP.NET Core
you'll need to spell it out:
var request = Context.Request;
@($"{ request.Scheme }://{ request.Host }{ request.Path }{ request.QueryString }")
Or you can add a using statement to your view:
@using Microsoft.AspNetCore.Http.Extensions
then
@Context.Request.GetDisplayUrl()
The _ViewImports.cshtml
might be a better place for that @using