Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current URL from ASP.NET code behind

Tags:

asp.net

My application is hosted on different servers, and I want to get the URL of the page on the current server.

How can you get this property in code behind?

like image 809
dotnetandsqldevelop Avatar asked Jul 22 '13 11:07

dotnetandsqldevelop


People also ask

How do I get the current URL in Blazor?

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

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.

What is URL in asp net?

ASP.NET Routing Overview. URL routing allows you to configure an application to accept request URLs that do not map to physical files. A request URL is simply the URL a user enters into their browser to find a page on your web site.


2 Answers

Another way to get URL from code behind file

public string FullyQualifiedApplicationPath
{
    get
    {
        //Return variable declaration
        var appPath = string.Empty;

        //Getting the current context of HTTP request
        var context = HttpContext.Current;

        //Checking the current context content
        if (context != null)
        {
            //Formatting the fully qualified website url/name
            appPath = string.Format("{0}://{1}{2}{3}",
                                    context.Request.Url.Scheme,
                                    context.Request.Url.Host,
                                    context.Request.Url.Port == 80
                                        ? string.Empty
                                        : ":" + context.Request.Url.Port,
                                    context.Request.ApplicationPath);
        }

        if (!appPath.EndsWith("/"))
            appPath += "/";

        return appPath;
    }
}

check this Link you will get more info.

like image 75
IMMORTAL Avatar answered Oct 03 '22 07:10

IMMORTAL


string url = HttpContext.Current.Request.Url.AbsoluteUri;

http://thehost.com/dir/Default.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;

/dir/Default.aspx

string host = HttpContext.Current.Request.Url.Host;

thehost.com

like image 31
YaakovHatam Avatar answered Oct 03 '22 07:10

YaakovHatam