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" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <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="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Default Page</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Label
       id="lblMessage"
       CssClass="message"
       Runat="server" />
   
   The original request was for:

<%=Request.RawUrl%>

   which got remapped to:

<%= Request.Path %>

   and the application relative version is:

<%= Request.AppRelativeCurrentExecutionFilePath %>

   </form>

</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>

 <urlMappings>
   <add
     url="~/Home.aspx"
     mappedUrl="~/Default.aspx"/>
 </urlMappings>

</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>


Request.RawUrl

   <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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <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>
   
   </form>

</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;
   }

}</source>