Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created October 22, 2025 03:08
Show Gist options
  • Select an option

  • Save karenpayneoregon/85cb38af74db5a99ae9cbca69c91da9b to your computer and use it in GitHub Desktop.

Select an option

Save karenpayneoregon/85cb38af74db5a99ae9cbca69c91da9b to your computer and use it in GitHub Desktop.
ASP.NET Core get current page name
public string CurrentPageName { get; private set; }
public void OnGet()
{
CurrentPageName = PageHelpers.GetCurrentPageName(HttpContext.Request);
}
/// <summary>
/// Provides helper methods for working with pages in an ASP.NET Core application.
/// </summary>
/// <remarks>
/// This class contains utility methods that assist in handling page-related operations,
/// such as retrieving the current page name from an HTTP request.
/// </remarks>
public class PageHelpers
{
/// <summary>
/// Retrieves the name of the current page based on the provided HTTP request.
/// </summary>
/// <param name="request">The <see cref="HttpRequest"/> object representing the current HTTP request. Cannot be <see langword="null"/>.</param>
/// <returns>
/// A <see cref="string"/> representing the name of the current page.
/// Returns "Index" if the request path is the root ("/"), otherwise returns the file name without its extension.
/// </returns>
public static string GetCurrentPageName(HttpRequest request)
{
string path = request.Path;
return path == "/" ? "Index" : Path.GetFileNameWithoutExtension(path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment