Created
October 22, 2025 03:08
-
-
Save karenpayneoregon/85cb38af74db5a99ae9cbca69c91da9b to your computer and use it in GitHub Desktop.
ASP.NET Core get current page name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public string CurrentPageName { get; private set; } | |
| public void OnGet() | |
| { | |
| CurrentPageName = PageHelpers.GetCurrentPageName(HttpContext.Request); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <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