ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,jQuery Plugins,jQuery UI,SSRS,XML,HTML,jQuery demos,code snippet examples.

Breaking News

  1. Home
  2. C #
  3. Current Page Name in C#

Current Page Name in C#

A Web site is a collection of some HTML documents. Each HTML document is called Web pages. In Web applications some times we need to identify the page name from a URL visited by the users. This article describes how to get the page name of a URL in asp.net C#. Summary of the article:
  • C# Code to Get the Current Page Name
  • C# Method to Get the Current Page Name
C# Code to Get the Current Page Name
By using C# language we can get the Web page name of a Web site. A sample C# code to detect current page name is given bellow:
string PageURL = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oFileInfo = new System.IO.FileInfo(PageURL);
string PageName = oFileInfo.Name;
We need to include the “System.IO” namespace.
The “FileInfo” is a built-in mechanism provided by .Net Framework that retrieves the information about a specific file or directory from the file system. These help us to detect the status of a file.
Here “FileInfo.Name” properties is used to get the name of the file
The “HttpContext” is a .Net Framework class used to specify an individual HTTP request. It provides the information about the HTTP request. It has also some methods and properties.
Here “HttpContext.Current” property is used. It is a static property that gets or sets the HttpContext object for the current HTTP request. It grabs the current Request, Response, Session and many more.
C# Method to Get the Current Page Name
Also we can build a method that will detect the Web page name from the URL. Method writing is more effective than direct coding. Because it provides numerous benefits. A method makes the code simpler and reduce development time. The same method can be used in different locations of the application. A sample C# method is given bellow:
public string GetPageName()
{
    string PageURL = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo oFileInfo = new System.IO.FileInfo(PageURL);
    string PageName = oFileInfo.Name;
    return PageName;
}

In the application, it is very important to know the File name Or Web page name. This name can be used in different purpose. This can be used to set or assign page wise security setting for a particular user.

No comments