Practical approach of getting Current and previous Page name from the URL

Practical approach of getting Current and previous Page name from the URL

In this blog we are going to see how we parse the Current and previous page name from the URL.

1.      Used Namespaces

            using System.Web;

2.      Getting Current Page name from the URL

internal static string GetURLName()

            {

                  Uri uri = HttpContext.Current.Request.Url;

                  return uri.Segments[uri.Segments.Length - 1].ToLower();

            }

3.      Getting Previous page name from URL

internal static string GetPreviousURLName()

            {

                  Uri uri = HttpContext.Current.Request.UrlReferrer;

                  return uri != null ? uri.Segments[uri.Segments.Length - 1].ToLower() : string.Empty;

            }

4.      Getting Current Page name from the URL without extension

internal static string GetURLFileName()

            {

                  Uri uri = HttpContext.Current.Request.Url;

                  return uri.Segments[uri.Segments.Length - 1].ToLower().Replace(".aspx", string.Empty);

            }

5.      Checking Invalid page

internal static bool InValidPage(string pageName)

            {

                  return GetURLName() != pageName;

            }

Thanks for reading this article. Have a nice day.

 

Next Recommended Reading Go Back to the previous page in ASP.NET