How do I get the full url of the page I am on in C#
I usually use Request.Url.ToString()
to get the full url (including querystring), no concatenation required.
How to get FULL URL in ASP.NET Core Razor Pages?
You can use the UriHelper
extension methods GetDisplayUrl()
or GetEncodedUrl()
to get the full URL from the request.
GetDisplayUrl()
Returns the combined components of the
request URL in a fully un-escaped form (except for the QueryString)
suitable only for display. This format should not be used in HTTP
headers or other HTTP operations.
GetEncodedUrl()
Returns the combined components of the
request URL in a fully escaped form suitable for use in HTTP headers
and other HTTP operations.
Usage:
using Microsoft.AspNetCore.Http.Extensions;
...
string url = HttpContext.Request.GetDisplayUrl();
// or
string url = HttpContext.Request.GetEncodedUrl();
Getting the full url of the current page with url hash on server side
there is no way to get hash content on server side because hash are never posted to the server
see this question for some tricks How to get Url Hash (#) from server side
How to get current page URL in MVC 3
You could use the Request.RawUrl
, Request.Url.OriginalString
, Request.Url.ToString()
or Request.Url.AbsoluteUri
.
How can I get the baseurl of site?
Try this:
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";
Getting the current URL that is shown in the browser
You can get it from the request in the httpcontext.
HttpContext.Current.Request.Url
Updated:
If you want to tell wether the current url is / or /default.aspx. You can use the RawUrl property of the request. This field will contain the whole url.
HttpContext.Current.Request.RawUrl
How to get URL path in C#
Don't treat it as a URI problem, treat it a string problem. Then it's nice and easy.
String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
String parentDirectory = originalPath.Substring(0, originalPath.LastIndexOf("/"));
Really is that easy!
Edited to add missing parenthesis.
Related Topics
How to Bind Parameters via Odbc C#
Best Programming Practice of Using Dropdownlist in ASP.NET MVC
C# How to Find If an Event Is Hooked Up
How to Initialize a List<T> to a Given Size (As Opposed to Capacity)
Render Razor View to String in ASP.NET Core
Asynchronous Task.Whenall with Timeout
What Is Imex Within Oledb Connection Strings
Change Border Color in Textbox C#
Lambda Expression Not Returning Expected Memberinfo
How to Change the Background Color of All Other Forms from One Form
How to Find Fqdn of Local MAChine in C#/.Net
Mvvm Light 5.0: How to Use the Navigation Service
What's the Difference Between the Webconfigurationmanager and the Configurationmanager
Get a Generic Method Without Using Getmethods
Why Isn't My Public Property Serialized by the Xmlserializer