Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL path in C#

Tags:

I want to get the all the path of URL except the current page of url, eg: my URL is http://www.MyIpAddress.com/red/green/default.aspx I want to get "http://www.MyIpAddress.com/red/green/" only. How can I get.I'm doing like

string sPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString; System.Web.HttpContext.Current.Request.Url.AbsolutePath;             sPath = sPath.Replace("http://", "");             System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);             string sRet = oInfo.Name;             Response.Write(sPath.Replace(sRet, "")); 

Its showing exception on new System.IO.FileInfo(sPath) as sPath contain "localhost/red/green/default.aspx" saying "The given path's format is not supported."

like image 645
Nomi Ali Avatar asked Nov 02 '13 06:11

Nomi Ali


People also ask

Can a URL be a file path?

So yes, file URIs are URLs.

What is request URL AbsoluteUri?

Url. AbsoluteUri is returning "http://www.somesite.com/default.aspx" , when the Url in the client's browser looks like "http://www.somesite.com/". This small diffrence is causing a redirect loop.

How do I get the current URL in Blazor?

Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.


1 Answers

Main URL : http://localhost:8080/mysite/page.aspx?p1=1&p2=2

Get different parts of URL in C#.

Value of HttpContext.Current.Request.Url.Host localhost  Value of HttpContext.Current.Request.Url.Authority localhost:8080  Value of HttpContext.Current.Request.Url.AbsolutePath /mysite/page.aspx  Value of HttpContext.Current.Request.ApplicationPath /mysite  Value of HttpContext.Current.Request.Url.AbsoluteUri http://localhost:8080/mysite/page.aspx?p1=1&p2=2  Value of HttpContext.Current.Request.RawUrl /mysite/page.aspx?p1=1&p2=2  Value of HttpContext.Current.Request.Url.PathAndQuery /mysite/page.aspx?p1=1&p2=2 
like image 101
Shibu Thomas Avatar answered Oct 16 '22 04:10

Shibu Thomas