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
相关文档:
这个属性是用来验证客户端用户的输入的,用来验证用户的输入中是否有危险字符的,这个属性的默认值为true,微软之所以这么做是为了提高asp.net程序的安全性,所以很多程序员即使不知道怎么来防御黑客的攻击,asp.net的一些默认属性等内容已经对安全进行了控制,这也是为什么asp.net的程序相对来说比较安全的原因!
既然这个属 ......
asp.net发布网站时三个选项的问题
发布网站的时候有三个选项:
第一个选项指定发布后是不是可以修改aspx文件,如果勾选,则发布后的网站行为基本与ASP.NET 1.1一致,只要没有增删修改控件,可以直接在服务器上修改aspx文件不用重新发布网站。
第二个选项指定是不是将每个aspx文件都编 ......
一、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 ......
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
namespace SQLHelper
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class SQLHelper
{
// 连接数据源
......
1.添加js函数
function TextBoxEntered(buttton) {
if (event.which || event.keyCode) {
if ((event.which == 13) || (event.keyCode == 13)) {
document.getElementById(button).click();
return false;
}
......