http://stackoverflow.com/questions/13869817/difference-in-physical-path-root-path-virutal-path-relative-virtual-path-app

第一个答案

In regards to an ASP.NET application I think of it like this:

Physical Path: OS path using drive/directory/file in which the actual app doesnt really use this path but if it did it would be mapped using a virtual path. A physical path is how the OS locates the resource/s ie: c:\\inetpub\wwwroot\aspnetapp The actual app only cares about paths relative to its root directory.

Root Path: This would be the URI or URL at the root of your aspnetapp or ~/Home/Index with proper route config (Not to be confused with Unix Root Directory naming convention).

http://www.yardpenalty.com could actually be the location of this example's physical path in terms of an OS/NOS.

Virtual Path or Relative Virtual Path: The path that the application identifies or is identified by from its Web server.

For instance, in IIS (or OWIN) you may have a resource directory for your images in folder c:\\inetpub\ftp\images but the developer maps this folder to the app like so... ~\Images.

So think of this as the ability to create a relative path to resources identifiable by your app and its users while physically located elsewhere.

I would imagine that using a virtual path under a root application would be helpful in development when there are one or more projects that the developer wishes to give the appearance of a single application under a single domain.

Absolute Path: The entire path to a resource.

Let's say you have a link that takes you to a specific route like this: <a href="http://www.yardpenalty.com/home/about"> About</a>.

If this link was in the layout or master page a relative path <a href="~/home/about">About</a> would be cleaner.

There are instances when you need to hard code an absolute path but it is typically wiser to use relative paths especially when development involves migrations.

第二个答案

Making Sense of ASP.NET Paths

ASP.Net includes quite a plethora过剩 of properties to retrieve path information about the current request, control and application.

There's a ton of大量 information available about paths on the Request object, some of it appearing to overlap and some of it buried several levels down, and it can be confusing to find just the right path that you are looking for.

To keep things straight I thought it a good idea to summarize the path options along with descriptions and example paths.

I wrote a post about this a long time ago in 2004 and I find myself frequently going back to that page to quickly figure out which path I’m looking for in processing the current URL.

Apparently a lot of people must be doing the same, because the original post is the second most visited even to this date on this blog to the tune of nearly 500 hits per day.

So, I decided to update and expand a bit on the original post with a little more information and clarification based on the original comments.

Request Object Paths Available

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

Assume a path like http://www.west-wind.com/webstore/admin/paths.aspx for the paths below where webstore is the name of the virtual.

1.ApplicationPath

Returns the web root-relative logical path to the virtual root of this app.

/webstore/

2.PhysicalApplicationPath

Returns local file system path of the virtual root for this app. 
c:\inetpub\wwwroot\webstore

3.PhysicalPath

Returns the local file system path to the current script or path
c:\inetpub\wwwroot\webstore\admin\paths.aspx

4.

Path 
FilePath 
CurrentExecutionFilePath

All of these return the full root relative logical path to the script page including path and scriptname.

CurrentExcecutionFilePath will return the ‘current’ request path after a Transfer/Execute call while FilePath will always return the original request’s path. 
/webstore/admin/paths.aspx

5.AppRelativeCurrentExecutionFilePath

Returns an ASP.NET root relative virtual path to the script or path for the current request. If in  a Transfer/Execute call the transferred Path is returned. 
~/admin/paths.aspx

6.PathInfo

Returns any extra path following the script name.

If no extra path is provided returns the root-relative path (returns text in red below). string.Empty if no PathInfo is available. 
/webstore/admin/paths.aspx/ExtraPathInfo

7.RawUrl

Returns the full root relative URL including querystring and extra path as a string. 
/webstore/admin/paths.aspx?sku=wwhelp40

8.Url

Returns a fully qualified URL including querystring and extra path. Note this is a Uri instance rather than string. 
http://www.west-wind.com/webstore/admin/paths.aspx?sku=wwhelp40

9.UrlReferrer

The fully qualified URL of the page that sent the request.

This is also a Uri instance and this value is null if the page was directly accessed by typing into the address bar or using an HttpClient based Referrer client Http header. 
http://www.west-wind.com/webstore/default.aspx?Info

10.Control.TemplateSourceDirectory

Returns the logical path to the folder of the page, master or user control on which it is called.

This is useful if you need to know the path only to a Page or control from within the control. For non-file controls this returns the Page path. 
/webstore/admin/

As you can see there’s a ton of information available there for each of the three common path formats:

  • Physical Path 
    is an OS type path that points to a path or file on disk.
  • Logical Path 
    is a Web path that is relative to the Web server’s root. It includes the virtual plus the application relative path.
  • ~/ (Root-relative) Path 
    is an ASP.NET specific path that includes ~/ to indicate the virtual root Web path. ASP.NET can convert virtual paths into either logical paths using Control.ResolveUrl(), or physical paths using Server.MapPath(). Root relative paths are useful for specifying portable URLs that don’t rely on relative directory structures and very useful from within control or component code.

You should be able to get any necessary format from ASP.NET from just about any path or script using these mechanisms.

~/ Root Relative Paths and ResolveUrl() and ResolveClientUrl()

ASP.NET supports root-relative virtual path syntax in most of its URL properties in Web Forms.

So you can easily specify a root relative path in a control rather than a location relative path:

<asp:Image runat="server" ID="imgHelp"  ImageUrl="~/images/help.gif" />

ASP.NET internally resolves this URL by using ResolveUrl("~/images/help.gif") to arrive at the root-relative URL of /webstore/images/help.gif which uses the Request.ApplicationPath as the basepath to replace the ~.

By convention any custom Web controls also should use ResolveUrl() on URL properties to provide the same functionality.

In your own code you can use Page.ResolveUrl() or Control.ResolveUrl() to accomplish the same thing:

string imgPath = this.ResolveUrl("~/images/help.gif");
imgHelp.ImageUrl = imgPath;

Unfortunately ResolveUrl() is limited to WebForm pages, so if you’re in an HttpHandler or Module it’s not available.

ASP.NET Mvc also has it’s own more generic version of ResolveUrl in Url.Decode:

<script src="<%= Url.Content("~/scripts/new.js") %>" type="text/javascript"></script> 

which is part of the UrlHelper class. In ASP.NET MVC the above sort of syntax is actually even more crucial than in WebForms due to the fact that views are not referencing specific pages but rather are often path based which can lead to various variations on how a particular view is referenced.

In a Module or Handler code Control.ResolveUrl() unfortunately is not available which in retrospect seems like an odd design choice – URL resolution really should happen on a Request basis not as part of the Page framework. Luckily you can also rely on the static VirtualPathUtility class:

string path = VirtualPathUtility.ToAbsolute("~/admin/paths.aspx");

VirtualPathUtility also many other quite useful methods for dealing with paths and converting between the various kinds of paths supported. One thing to watch out for is that ToAbsolute() will throw an exception if a query string is provided and doesn’t work on fully qualified URLs. I wrote about this topic with a custom solution that works fully qualified URLs and query strings here (check comments for some interesting discussions too).

Similar to ResolveUrl() is ResolveClientUrl() which creates a fully qualified HTTP path that includes the protocol and domain name. It’s rare that this full resolution is needed but can be useful in some scenarios.

补充~/是用来访问web的根目录的

difference in physical path, root path, virutal path, relative virtual path, application path and aboslute path?的更多相关文章

  1. parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/applicationContext.xml] 解决方案

    parsing XML document from class path resource [config/applicationContext.xml]; nested exception is j ...

  2. 使用QFileInfo类获取文件信息(在NTFS文件系统上,出于性能考虑,文件的所有权和权限检查在默认情况下是被禁用的,通过qt_ntfs_permission_lookup开启和操作。absolutePath()必须查询文件系统。而path()函数,可以直接作用于文件名本身,所以,path() 函数的运行会更快)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Amnes1a/article/details/65444966QFileInfo类为我们提供了系统无 ...

  3. Application windows are expected to have a root view controller at the end of application launch

    今天把Xcode升级了,模拟器 用的12.1的系统,运行时发现项目总是崩溃,采用9.3系统的测试机发现错误日志如下: Application windows are expected to have ...

  4. for xml path(''),root('')

    ,,'') SELECT top 10 ROW_NUMBER()OVER(ORDER BY OperationID) as 'Message/MessageId', OperationID as 'I ...

  5. rethinking virtual network embedding..substrate support for path splitting and migration阅读笔记

    1.引言 网络虚拟化, 1.支持同一个底层网络有多种网络架构,每种架构定制一个应用或用户社区. 2.也可以让多个服务提供者在共同的物理基础设施上定制端到端的服务.如Voice over IP(VoIP ...

  6. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  7. ansible文件模块使用

    1. 文件组装模块-assemble assemble主要是将多份配置文件组装为一份配置文件. 参数 必填 默认 选项 说明 Backup 否 No Yes/no 是否创建备份文件,使用时间戳 Del ...

  8. 257. Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  9. 利用ItextPdf、core-renderer-R8 来生成PDF

    近期因为工作上的须要,须要做一个简历产品的下载功能,而下载的形式要去为PDF,内容要求为整个简历的内容,并且格式上要求和简历的格式排版时一致的!前期调研.开发,最后測试上线.差点儿相同花了7天的时间. ...

随机推荐

  1. (WC2016模拟十一)【BZOJ4695】最假女选手

    ps:好久没更博啦……这几天连着有模拟赛,等初赛前后休息的时候来疯狂补坑吧……顺便补一下前面的数论啥的? 题解: mdzz我场上写了个15分暴力长度跟标算差不多... 线段树大法好啊!这题听说很多人做 ...

  2. luogu P2137 Gty的妹子树(分块,主席树)

    询问的化我们可以建主席树.然后修改?,树套树...,最后插入?炸了. 所以我们对操作进行分块. 我们先对整棵树建一个主席树.修改,插入我们先记录下来.然后询问的时候先对主席树查询,然后暴力遍历我们记录 ...

  3. redis搭建与安装2

    第一步redis安装:1.首先确认下载包为64位的还是32位的2.下载http://code.google.com/p/servicestack/downloads3.解压下载包得到以下文件:cygw ...

  4. RHEL8.0-beta-1.ISO

    https://pan.baidu.com/s/1Yh_xuz39xGRrCtGtwhuqQg RHEL-8.0-beta-1-x86_64-dvd.iso       文件名: E:\rhel-8. ...

  5. 记录python之递归函数

    函数move(n,a,b,c)的定义是将n个圆盘从a借助b移动到c. def move(n,a,b,c): if n==1: print a,'-->',c move (n-1,a,c,b) p ...

  6. [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)

    P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...

  7. WPF 获取应用的所有窗口

    原文:WPF 获取应用的所有窗口 本文告诉大家如何获取应用内的所有窗口,无论这些窗口有没显示 在 WPF 可以通过 Application.Current.Windows 列举应用的所有窗口 fore ...

  8. HTTP——学习笔记(2)

    HTTP协议通信双方一定是客户端和服务器端,而且一定是由客户端发出请求,由服务器接受请求 客户端发送的报文的构成: 服务器端收到请求后响应的报文构成: 客户端向服务器端发送请求有多种方法: get:获 ...

  9. Java中四种复制数组的方法

    JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...

  10. [Mobx] Using mobx to isolate a React component state

    React is great for diffing between Virtual-DOM and rendering it to the dom. It also offers a naïve s ...