ASP.NET Tutorial/Page Lifecycle/URL

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Determine the current URL:

   <source lang="csharp">

Request.RawUrl returns the original URL (before being remapped). Request.Path returns the current URL (after being remapped). Request.AppRelativeCurrentExecutionFilePath returns the application relative URL (after being remapped). <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""> <script runat="server">

   void Page_Load()
       if (String.rupare(Request.Path, Request.RawUrl, true) != 0)
           lblMessage.Text = "The URL to this page has changed, " +
               "please update your bookmarks.";

</script> <html xmlns="" > <head runat="server">

   <title>Default Page</title>

</head> <body>

   <form id="form1" runat="server">
       Runat="server" />
   The original request was for:


   which got remapped to:

<%= Request.Path %>

   and the application relative version is:

<%= Request.AppRelativeCurrentExecutionFilePath %>


</body> </html></source>

Remapping URLs in your application"s web configuration file

   <source lang="csharp">

Remap the Home.aspx page to the Default.aspx page. File: Web.Config <configuration> <system.web>


</system.web> </configuration> The tilde character (~) represents the current application root. A forward slash (/) at the start of a URL represents the website root.</source>


   <source lang="csharp">

<%@ Page Language="C#" Trace="true" TraceMode="SortByTime" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HelloWorldCodeBehind" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" > <head runat="server">

   <title>Hello World Inline</title>

</head> <body>

   <form id="form1" runat="server">

Hello World

   The date is 
   <asp:Label ID="labDate" runat="server"></asp:Label>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class HelloWorldCodeBehind : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
      labDate.Text = Request.RawUrl;