易截截图软件、单文件、免安装、纯绿色、仅160KB

ASP.NET 如何取得 Request URL 的各個部分?


我們在開發網頁應用程式,時常需要去解析網址(Request.Url)的每個片段,進行一些判斷。例如說 "http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
",我們想要取得網址裡第一層目錄的名字(News)用以判斷不同的頁面標題(Page Title)。
我看很多人都用字串的 IndexOf 方法與 Substring 方法:
Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1)-1)
這實在太埋沒 .NET 的強大設計了,事實上在 Request 物件就已經提供很多方便的屬性(Property)可供取得網址的片段。
底下這張表就是各種跟 Browser Request 的網址相關的屬性與用法:
網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath
/
Request.PhysicalPath
D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath)
D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath
D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath)
Content.aspx
Request.CurrentExecutionFilePath
/News/Press/Content.aspx
Request.FilePath
/News/Press/Content.aspx
Request.Path
/News/Press/Content.aspx/123
Request.RawUrl
/News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath
/News/Press/Content.aspx/123
Request.Url.AbsoluteUri
http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme
http
Request.Url.Host
localhost
Request.Url.Port
1897
Request.Url.Authority
localhost:1897
Request.Url.LocalPath
/News/Press/Content.aspx/123
Request.PathInfo
/123
Request.Url.PathAndQuery
/News/Press/Content.aspx/123?id=1
Request.Url.Query
?id=1
Request.Url.Fragment
 
Request.Url.Segments
/
News/
Press/
Content.aspx/
123

所以當你看了這張表之後,你還會想用 Request.Url.PathAn


相关文档:

VFP如何调用ASP.NET Web服务中的DataSet

一、ASP.NET Web Service代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
 
namespace WebService1
{
    /// <sum ......

c# asp.net 字符串加密解密的类


using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Security.Cryptography;//Cryptography密码术   
namespace DAL   
{   
    public class Enc ......

asp.net数据库操作类(存储过程处理)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
namespace SQLHelper
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class SQLHelper
 {
  // 连接数据源
......

asp.net数据库操作类(不含存储过程)

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace class_new
{
 /// <summary>
 /// DataClass 的摘要说明。
 /// </summary>
 public class DataClass
 {
  private string strConnection="";
 &n ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号