Home > front end >  Can I get the current screen name on asp.net without strict encoding (Hard coding)?
Can I get the current screen name on asp.net without strict encoding (Hard coding)?

Time:12-27

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.

CodePudding user response:

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

CodePudding user response:

Please try this code

string ScreenName = Path.GetFileName(Request.Path);  

CodePudding user response:

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.

  • Related