惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
腾讯CDC
博客园_首页
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗

博客园 - ProgrammingBookWorm

多层结构来开发ASP.NET程序 在.NET下多层架构企业管理系统的开发 VS2008中文试用版的破解 在ASP.NET 2.0中操作数据::使用ObjectDataSource展现数据 HTTP服务器状态代码定义(Status Code Definitions) CodeSmith快速向导(翻译) 利用.net 2.0中的TreeView控件与数据库绑定,生成无限级的树目录 用户控件中使用客户端脚本的控件名称问题 用户中心 - 博客园 图解Windows2003邮件服务器 JScript .NET Fundamentals JScript IntelliSense in Visual Studio Orcas JScript Debugging in Visual Web Developer Orcas javascript中String 对象属性和方法 Window.Open详解 Get Query String variables in JavaScript ASP.NET Script Exploits Overview ASP.NET / How to: Implement Simple Forms Authentication How to: Implement Simple Forms Authentication
Making sense of ASP.Net Paths
ProgrammingBookWorm · 2008-01-15 · via 博客园 - ProgrammingBookWorm

Making sense of ASP.Net Paths


February 19, 2004 @ 2:06 pm

ASP.Net a plethora of properties to retrieve path information about the current request, control and application. To keep things straight I thought it'd be a good idea to summarize those options briefly along with describing some common scenarios of how they might be used.

Here's a list of the Path related properties on the Request object (and the Page object):

Request Property

Function and Example

ApplicationPath

Returns the a Web server relative path to your application root

/WestwindWebStore/

PhysicalApplicationPath

Returns a local file system path to your application root

D:"inetpub"wwwroot"WestWindWebStore"

PhysicalPath

Returns the full file system path to the currently executing script

D:"inetpub"wwwroot"WestWindWebStore"Item.aspx

CurrentExecutionFilePath

FilePath

Path

In most situations all of these return the virtual path to the currently executing script relative to the Web Server root.

/WestwindWebStore/item.aspx

PathInfo

Returns any extra path following the script name. Rarely used – this value is usually blank.

/WestwindWebStore/item.aspx/ExtraPathInfo

RawUrl

Returns the application relative URL including querystring or pathinfo

/WestwindWebStore/item.aspx?sku=WWHELP30

Url

Returns the fully qualified URL including domain and protocol

http://www.west-wind.com/Webstore/item.aspx?sku=WWHELP30  

Page.TemplateSourceDirectory

Control.TemplateSourceDirectory

Returns the virtual path of the currently executing control (or page). Very useful if you need to know the location of your ASCX control instead of the location of the page.

/WestwindWebStore/admin

Note though that there is nothing that returns to you just the name of the virtual path of the current page without the script name, which to me seems strange. I would expect Path to do this but instead you’ll have to strip of the script name:

string VirtualPath = Request.Path.Substring(0,Request.Path.LastIndexOf("/")+1 );

Between these settings you can get all the information you may need to figure out where you are at and to build new Url if necessary. If you need to build a URL completely from scratch you may need a few more: Server Variables:

Server Variable

Function and Example

SERVER_NAME

The name of the domain or IP address

SERVER_PORT

The port that the request runs under – if other than 80 you’ll have to build that into the URL

SERVER_PORT_SECURE

Determines whether https: was used

If you’re building a URL from scratch you might use something like this:

string Port = Request.ServerVariables["SERVER_PORT"];

if (Port == null || Port == "80" || Port == "443")

      Port = "";

else

      Port = ":" + Port;

string Protocol = Request.ServerVariables["SERVER_PORT_SECURE"];

if (Protocol == null || Protocol == "0")

      Protocol = "http://";

else

      Protocol = "https://";

// *** Figure out the base Url which points at the application's root

this.BasePath = Protocol + Request.ServerVariables["SERVER_NAME"] +

                            Port + Request.ApplicationPath;

From there you can add Request.QueryString etc. as needed. But in most situations it might just be easier to use the Request.Url and manipulate the string the way you need to to get the path. Watch out for the URL not always returning port information though!

Relative Paths and Server Controls

The most common use for those variables above is to get the application base path to create relative URLS. If you’ve ever built user controls I’m sure you will have found out the hassles that go with relative paths for images or stylesheets if you move the User or custom Server Control into a different directory. For components you always need to build off this base path and then append the relative path to links as necessary.

Note that ASP.Net’s internal controls all support the ~ as an Application base path designator whereever you can provide a link. For example in the Image Control you can say:

MyImage.ImageUrl = "~/images/stop.gif";

which will always load the image out of the application’s images directory regardless where the current page lives. If you create controls that require paths you should also always support this convention. It’s pretty easy to implement with code like this:

///

/// Fixes up URLs that include the ~ starting character and expanding
/// to a full server relative path

///

///

the URL to fix up

public static string FixupUrl(string Url)

{

      if (Url.StartsWith("~") )

            return  (HttpContext.Current.Request.ApplicationPath +
                    Url.Substring(1)).Replace("//","/");

      return Url;

}

You can also use Control.ResolveUrl() which returns a fully qualified URL from a partial or relative path.