How to Get the Full Url of the Page I am on in C#

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



Leave a reply



Submit