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.
You can use the --urls argument like this dotnet run --urls "http://localhost:8080" .
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.
Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With