Get the current page url by C#

How to get Current Page's Url in asp.net using C# code:



In this post we take a Example in this we want to get and display the current page url. Get Paths and URL fragments from the HttpRequest object?  Path and url information related to the application and the current request.  Sometimes, we want to get the current page URL that is displayed in the browser URL window on page. For example if you want to let your visitors submit a blog post on your blogging website you need to get that same exact URL.

Get Current Page URL Address in Asp.net:

Server.UrlEncode(Request.Url.ToString()); 

How to retrieve the URL of the current Page in ASP.NET:

Take a button control and a label control on page like this
<asp:Button ID="Button1" runat="server"
                    OnClick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"/>
Now on the button click event we want to  display page current url in label.

ASP: How to Get the Current Page URL:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ckeck_url_by_code.aspx.cs" Inherits="ckeck_url_by_code" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">


    <title>get current page url in asp.net</title>
    <style type="text/css">
        .style1
        {
            color#990000;
            font-sizex-large;
            text-decorationunderline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
    <div>
        <span class="style1"><strong>Enter your name and click
        on button:<br />
        </strong></span>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server"


                    OnClick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"/>

    </div>
   
    </div>
    </form>
</body>
</html>

Current Page in ASP.NET C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ckeck_url_by_code : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Server.HtmlEncode(TextBox1.Text)
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(TextBox1.Text))
{
            // Access the HttpServerUtility methods through
            // the intrinsic Server object.
            Label1.Text = "Welcome in This Site, <h1>"
                         +

                         + "</h1>.<br/> <h1>your current page url is</h1><br/> "

                         + Server.UrlEncode(Request.Url.ToString());

            // In this label we store given text message with current pagr url

        }
    }
}
Example of getting Url:

Get the current page url by C#
 Get the current page url 

Comments

Post a Comment