skip to Main Content

Any way to get the current screen name of asp.net without hard coding?

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

I tried this and got the full url.

2

Answers


  1. Chosen as BEST ANSWER

    I found a code. For me the string path is good

        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        // http://localhost:1302/TESTERS/Default6.aspx
    
        string path = HttpContext.Current.Request.Url.AbsolutePath;
        // /TESTERS/Default6.aspx
    
        string host = HttpContext.Current.Request.Url.Host;
        // localhost
    

  2. If you want to get the domain name from the url, use the following code:

    string[] hostParts = new System.Uri(sURL).Host.Split('.');
    string domain = String.Join(".", hostParts.Skip(Math.Max(0, hostParts.Length - 2)).Take(2));
    

    or :

    var host = new System.Uri(sURL).Host;
    var domain = host.Substring(host.LastIndexOf('.', host.LastIndexOf('.') - 1) + 1);
    

    where "sURL" is your URL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search