認証が有効なサイトでも、Request時のURLのUserInfoは空になるようです。
ASP.NETアプリケーションでリクエストURL(現在アクセスしているページのURL)を取得するコードを紹介します。
ASP.NETアプリケーションでリクエストURLを取得するには、
を用います。
プロパティ名 | 取得できる情報 |
---|---|
ApplicationPath | アプリケーションが配置されているパス |
FilePath | 現在アクセスしているページのファイルパス |
Path | 現在アクセスしているページのファイルパス(拡張パスを含む) |
PathInfo | 拡張パス |
PhysicalApplicationPath | マシン上でのファイルの配置ディレクトリのパス |
PhysicalPath | マシン上でのファイルのパス |
RawUrl | ホスト名を除くURLの文字列 |
Url.AbsolutePath | 現在アクセスしているページのファイルパス |
Url.AbsoluteUri | 現在アクセスしているページのURI |
Url.Host | 現在アクセスしているURLのホスト名 |
Url.LocalPath | 現在アクセスしているURLのローカルパス (ファイル名と拡張パス) |
Url.OriginalString | 現在アクセスしているURLのもともとの文字列 |
Url.Query | URLのパラメータ |
Url.Scheme | URLのスキーマ |
Url.UserInfo | 認証情報 |
下図のUIを作成します。
aspxファイルのコードは下記の通りです。
[|default.aspx]|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="RequestURL._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
<!--
.table tr:nth-child(odd) {
background-color:#cdeaff;
}
.table tr:nth-child(even) {
background-color:#f0f5ff;
}
-->
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server" CssClass="table"></asp:Table>
</div>
</form>
</body>
</html>
以下のコードを記述します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RequestURL
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AddTable("ApplicationPath", Request.ApplicationPath);
AddTable("FilePath", Request.FilePath);
AddTable("Path", Request.Path);
AddTable("PathInfo", Request.PathInfo);
AddTable("PhysicalApplicationPath", Request.PhysicalApplicationPath);
AddTable("PhysicalPath", Request.PhysicalPath);
AddTable("RawUrl", Request.RawUrl);
AddTable("Url.AbsolutePath", Request.Url.AbsolutePath);
AddTable("Url.AbsoluteUri", Request.Url.AbsoluteUri);
AddTable("Url.Host", Request.Url.Host);
AddTable("Url.LocalPath", Request.Url.LocalPath);
AddTable("Url.OriginalString", Request.Url.OriginalString);
AddTable("Url.Query", Request.Url.Query);
AddTable("Url.Scheme", Request.Url.Scheme);
AddTable("Url.UserInfo", Request.Url.UserInfo);
}
private void AddTable(string caption, string value)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = caption;
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = value;
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}
}
}
プロジェクトを実行します。
http://localhost:50571/default.aspx
上記のURLにアクセスした場合、下図の結果が表示されます。
http://localhost:50571/default.aspx?q=Penguin
上記のURLにアクセスした場合、下図の結果が表示されます。
http://localhost:50571/default.aspx/items/book?q=Penguin
上記のURLにアクセスした場合、下図の結果が表示されます。拡張パスが"PathInfo"に設定されます。