Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get application url from current request

I am writing a c# application.

I am accessing a page eg http://dev.mysite.com/page.aspx

How can I retrieve from the current context this http://dev.mysite.com/

I want to use this when creating url's in different environments so need to read it from the current request context.

like image 458
amateur Avatar asked Jun 18 '11 16:06

amateur


People also ask

How do I get the current URL in .NET core?

You can use the --urls argument like this dotnet run --urls "http://localhost:8080" .

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 find the current URL in razor view?

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


1 Answers

Uri uri = new Uri("http://dev.mysite.com/page.aspx");
string authority = uri.GetLeftPart(UriPartial.Authority);
// authority will equal to http://dev.mysite.com

or if you are inside this page.aspx you could directly use the Request.Url property:

string authority = Request.Url.GetLeftPart(UriPartial.Authority);
like image 110
Darin Dimitrov Avatar answered Sep 22 '22 21:09

Darin Dimitrov