C# ./ , ../ , ../../ , ~/ 在asp.net中的文件路径(URL)之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19272954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:33:50  来源:igfitidea点击:

Different between ./ , ../ , ../../ , ~/ on file path(URL) in asp.net

c#asp.neturlbrowserpath

提问by Ramesh Rajendran

I have a script file .

我有一个脚本文件。

<script src="~/Scripts/angular.js"></script>

See the path is ~/Script. But if I Entered ../../instead of ~/,Also the process are working same .

看到路径是~/Script。但是如果我输入../../而不是~/,那么这个过程也是一样的。

My website URL like : https://sample.com/Scripts/angular.js

我的网站网址如下: https://sample.com/Scripts/angular.js

If I entered ../../in before Scripts,then it's automatically change previous URL(https://sample.com/Scripts/angular.js).

如果我../../之前输入过Scripts,那么它会自动更改以前的 URL (https://sample.com/Scripts/angular.js)

What is the url process ? And how can its automatically changed? and please tell about the Different between ./, ../ , ../../ , ~/ ,/Scripts ,Scripts?

网址流程是什么?它如何自动更改?并请谈谈两者之间的区别./, ../ , ../../ , ~/ ,/Scripts ,Scripts

采纳答案by David

These path components are shortcuts with specific meanings:

这些路径组件是具有特定含义的快捷方式:

  • .means the current path level (so if you're on index.aspxand you reference ./style.cssthen the latter would have to be in the same folder as the former)
  • ..means one path level up (so if you're on /somefolder/index.aspxand you reference ../style.cssthen the latter would have to be in the parent folderof someFolder)
  • /means the root level (so /style.cssis the same as http://www.mysite.com/style.css)
  • ~in ASP.NET means the server-side application root (so ~/index.aspxwould be translated to the URL of the index.aspxfile that's in the application's root)
  • .表示当前路径级别(因此,如果您打开index.aspx并引用,./style.css则后者必须与前者位于同一文件夹中)
  • ..表示向上一级路径(因此,如果您打开/somefolder/index.aspx并引用,../style.css则后者必须位于 的父文件夹someFolder
  • /表示根级别(因此/style.css与 相同http://www.mysite.com/style.css
  • ~在 ASP.NET 中表示服务器端应用程序根目录(因此~/index.aspx将转换为应用程序根目录中index.aspx文件的 URL )

There are a number of things to note here:

这里有很多事情需要注意:

  • There is a difference between serverpaths and clientpaths. For example, from the web browser's perspective there's no "application root." A web browser wouldn't know what to do with ~. That can only be used in paths which are pre-processed in server-side components. The server-side components would then know to translate that into a client-visible path based on the current location of the application relative to the web server.
  • Parent path specifiers (..) have no limit. The root's parent is considered the root. So if you're on http://www.mysite.com/someFolder/index.aspxand you reference ../../../../style.cssit will go to http://www.mysite.com/style.css.
  • The browser also translates paths for you. This is one of the differences between the "page source" and the "DOM." Your page source may have a reference to ../somePage.aspx, but when you hover over it with your mouse the browser indicates that it's http://www.mysite.com/somePage.aspx. This is because the browser has converted the relative pathof the former into the absolute pathof the latter.
  • 服务器路径和客户端路径之间存在差异。例如,从 Web 浏览器的角度来看,没有“应用程序根”。Web 浏览器不知道如何处理~. 这只能用于在服务器端组件中预处理的路径。然后,服务器端组件将知道根据应用程序相对于 Web 服务器的当前位置将其转换为客户端可见的路径。
  • 父路径说明符 ( ..) 没有限制。根的父级被视为根。因此,如果您打开http://www.mysite.com/someFolder/index.aspx并引用../../../../style.css它,它将转到http://www.mysite.com/style.css.
  • 浏览器还会为您翻译路径。这是“页面源”和“DOM”之间的区别之一。您的页面源可能有对 的引用../somePage.aspx,但是当您将鼠标悬停在它上面时,浏览器会指示它是http://www.mysite.com/somePage.aspx。这是因为浏览器已经将前者的相对路径转换成了后者的绝对路径

回答by opalenzuela

Let's see...

让我们来看看...

 .    = this directory
 ..   = the parent directory
 ../  = the parent directory
 ~/   = the user's home directory or the application's, in ASP
 /    = the root directory

 ../../ = the parent's parent directory 

and so on.

等等。

BTW, this works for all Linux/Unix systems.

顺便说一句,这适用于所有 Linux/Unix 系统。