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 的網址相關的屬性與用法:
所以當你看了這張表之後,你還會想用 Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1)-1) 這種寫法嗎? 用這樣寫 Request.Url.Segments[1].Replace("/", "") 不是又短又直覺嗎? ^_^ 以下是產生以上表格的程式碼: protected void Page_Load(object sender, EventArgs e) StringBuilder sb = new StringBuilder(); sb.Append("<table cellpadding=3 cellspacing=0 border=1>"); sb.Append("<tr><td colspan=2>"); // Request.ApplicationPath // Request.PhysicalPath // System.IO.Path.GetDirectoryName(Request.PhysicalPath) // Request.PhysicalApplicationPath // System.IO.Path.GetFileName(Request.PhysicalPath) // Request.CurrentExecutionFilePath // Request.FilePath // Request.Path // Request.RawUrl // Request.Url.AbsolutePath // Request.Url.AbsoluteUri // Request.Url.Scheme // Request.Url.Host // Request.Url.Port // Request.Url.Authority // local Request.Url.LocalPath // Request.PathInfo // Request.Url.PathAndQuery // Request.Url.Query // Request.Url.Fragment // Request.Url.Segments sb.Append("</table>"); ltlTable.Text = sb.ToString(); |
||||||||||||||||||||||||||||||||||||||||||||
Request.url用法的更多相关文章
- ASP.NET取得Request URL的各个部分
我們在開發網頁應用程式,時常需要去解析網址(Request.Url)的每個片段,進行一些判斷.例如說 "http://localhost:1897/News/Press/Content.as ...
- nodejs request模块用法
request是服务端发起请求的工具包 1.安装 npm i request 2.基本用法 默认是GET请求 var request = require('request'); request('您的 ...
- ASP.NET中Request.RawUrl、Request.Url的区别
如果访问的地址是: http://hovertree.com/guestbook/addmessage.aspx?key=hovertree%3C&n=myslider#zonemenu 那么 ...
- Request.url请求路径的一些属性
Request.url请求路径的一些属性1,Request.UrlReferrer.AbsolutePath=获取URL的绝对路径例:"/Manager/Module/OfficialMan ...
- Request.IsLocal与Request.Url.IsLoopback的区别
均在服务器上访问时: http://localhost:17810 Request.IsLocal => trueRequest.Url.IsLoopback => true http:/ ...
- css cursor url用法格式详解
css cursor url用法格式:css:{cursor:url('图标路径'),auto;} //IE,FF,chrome浏览器都可以 实例代码:html{cursor: url("h ...
- 转载 ASP.NET中如何取得Request URL的各个部分
转载原地址 http://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequ ...
- Request url 各种属性值
網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.Phys ...
- Asp.net中Request.Url的各个属性对应的意义介绍
Asp.net中Request.Url的各个属性对应的意义介绍 本文转载自 http://www.jb51.net/article/30254.htm 网络上关于Request.Url的说明已经很多也 ...
随机推荐
- 3高并发server:多路IO之epoll
1 epoll epoll是Linux下多路复用IO接口select/poll的增强版本号,它能显著提高程序在大量并.发连接中仅仅有少量活跃的情况下的系统CPU利用率,由于它会复用文件描写叙述符 ...
- RHCA-RH442-Linux系统性能调优 (学习)
RHCA-RH442-Linux系统性能调优
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- 简单的代码实现的炫酷navigationbar
动图 技术原理: 当你下拉scrollview的时候,会监听scrollview的contentOffset来调整头部背景图片的位置,通过CGAffineTransformMakeTranslatio ...
- 关于a标签的链接的表现形式
target属性:self在自身标签页里打开 blank在新标签页中打开 1. 链接到其它地址位置.html文档等 <a href="1.html"></a&g ...
- Django模型之Meta选项详解
Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.而可用的选项大致包含以下几类 abstract 这个属性是定义当前的模型是不是一个抽象类.所谓抽象类是不会对应数据 ...
- (转)system()函数
[C/C++]Linux下system()函数引发的错误 今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...
- 国际化 native2ascii用法
cmd下输入: native2ascii -encoding GBK(需要编译成哪种语言) (中文文件路劲) (英文文件路劲) 其他固定 例如 native2ascii -encoding GBK C ...
- 【HOJ1356】【Miller_rabin素性测试】Prime Judge
Given a positive integer, your job is writing a program to determine whether it is a prime number or ...
- topcoder算法练习2
Problem Statement In most states, gamblers can choose from a wide variety of different lottery ...