Server.MapPath (“.”), Server.MapPath (“~”), Server.MapPath (@ “\”), Server.MapPath (“/”). Quelle est la différence?
-
07-07-2019 - |
Question
Quelqu'un peut-il expliquer la différence entre Server.MapPath (".")
, Server.MapPath ("~ ~")
, Serveur. MapPath (@ """)
et Server.MapPath ("/ /")
?
La solution
Server.MapPath spécifie le chemin relatif ou virtuel vers lequel mapper vers un répertoire physique .
-
Server.MapPath (".")
1 renvoie le répertoire physique actuel du fichier (par exemple, aspx) en cours d'exécution -
Server.MapPath (" .. ")
renvoie le répertoire parent -
Server.MapPath (" ~ ")
renvoie le chemin physique à la racine de l'application -
Server.MapPath (")
renvoie le chemin d'accès physique à la racine du nom de domaine (n'est pas nécessairement identique à la racine de l'application)
Un exemple:
Supposons que vous ayez dirigé une application de site Web ( http://www.example.com/
) sur
C:\Inetpub\wwwroot
et installé votre application de magasin (sous-site Web en tant que répertoire virtuel dans IIS, marqué en tant qu’application) dans
D:\WebApps\shop
Par exemple, si vous appelez Server.MapPath ()
dans la requête suivante:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
alors:
-
Server.MapPath (".")
1 renvoieD: \ WebApps \ shop \ produits
-
Server.MapPath (" .. ")
renvoieD: \ WebApps \ shop
-
Server.MapPath (" ~ ")
renvoieD: \ WebApps \ shop
-
Server.MapPath ("/")
renvoieC: \ Inetpub \ wwwroot
-
Server.MapPath ("/ shop")
renvoieD: \ WebApps \ shop
Si Path commence par une barre oblique ( /
) ou une barre oblique inverse ( \
), le MapPath ()
renvoie un chemin si Path était un chemin virtuel complet.
Si Path ne commence pas par une barre oblique, MapPath ()
renvoie un chemin relatif au répertoire de la demande en cours de traitement.
Remarque: en C #, @
est l'opérateur de chaîne littérale mot à mot, ce qui signifie que la chaîne doit être utilisée " telle qu'elle est " et ne pas être traité pour les séquences d'échappement.
Notes de bas de page
-
Server.MapPath (null)
etServer.MapPath (")
sera génère également cet effet .
Autres conseils
Juste pour développer un peu la réponse de @ splattne:
MapPath (string virtualPath)
appelle les éléments suivants:
public string MapPath(string virtualPath)
{
return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}
MapPath (VirtualPath virtualPath)
appelle à son tour MapPath (VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping)
qui contient les éléments suivants:
//...
if (virtualPath == null)
{
virtualPath = VirtualPath.Create(".");
}
//...
Donc, si vous appelez MapPath (null)
ou MapPath (""))
, vous appelez effectivement MapPath (".") / code>
1) Server.MapPath ("." "
): renvoie le" Répertoire physique actuel ". du fichier (par exemple, aspx
) en cours d'exécution.
Ex. Supposons que D: \ WebApplications \ Collage \ Departments
2) Server.MapPath ("..")
- Renvoie le "Répertoire parent"
Ex. D: \ WebApplications \ Collage
3) Server.MapPath ("~" "
): renvoie le" chemin d'accès physique à la racine de l'application "
Ex. D: \ WebApplications \ Collage
4) Server.MapPath ("/")
: renvoie le chemin d'accès physique à la racine du nom de domaine
Ex. C: \ Inetpub \ wwwroot