ASP.NET Tutorial/Page Lifecycle/URL
Версия от 15:30, 26 мая 2010; (обсуждение)
Determine the current URL:
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">
<div>
<asp:Label
id="lblMessage"
CssClass="message"
Runat="server" />
The original request was for:
<blockquote>
<%=Request.RawUrl%>
</blockquote>
which got remapped to:
<blockquote>
<%= Request.Path %>
</blockquote>
and the application relative version is:
<blockquote>
<%= Request.AppRelativeCurrentExecutionFilePath %>
</blockquote>
</div>
</form>
</body>
</html>
Remapping URLs in your application"s web configuration file
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.
Request.RawUrl
<%@ 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">
<h1>Hello World</h1>
The date is <em>
<asp:Label ID="labDate" runat="server"></asp:Label>
</em>
</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;
}
}