Server.MapPath (“.”), Server.MapPath (“~”), Server.MapPath (@ “\”), Server.MapPath (“/”). Qual è la differenza?

StackOverflow https://stackoverflow.com/questions/275781

Domanda

Qualcuno può spiegare la differenza tra Server.MapPath (". ") , Server.MapPath (" ~ ") , Server. MapPath (@ " \ ") e Server.MapPath (" / ") ?

È stato utile?

Soluzione

Server.MapPath specifica il percorso relativo o virtuale per mappare a una directory fisica .

  • Server.MapPath(".") 1 restituisce la directory fisica corrente del file (ad es. aspx) in esecuzione
  • Server.MapPath (" .. ") restituisce la directory padre
  • Server.MapPath (" ~ ") restituisce il percorso fisico alla radice dell'applicazione
  • Server.MapPath (" / ") restituisce il percorso fisico alla radice del nome di dominio (non è necessariamente uguale alla radice dell'applicazione)

Un esempio:

Supponiamo che tu abbia indicato un'applicazione del sito Web ( http://www.example.com/ ) su

C:\Inetpub\wwwroot

e installato l'applicazione del tuo negozio (Web secondario come directory virtuale in IIS, contrassegnato come applicazione) in

D:\WebApps\shop

Ad esempio, se si chiama Server.MapPath () nella seguente richiesta:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

quindi:

  • Server.MapPath(".") 1 restituisce D:\WebApps\shop\products
  • Server.MapPath (" .. ") restituisce D:\WebApps\shop
  • Server.MapPath (" ~ ") restituisce D:\WebApps\shop
  • Server.MapPath (" / ") restituisce C:\Inetpub\wwwroot
  • Server.MapPath (" / shop ") restituisce D:\WebApps\shop

Se Path inizia con una barra ( / ) o una barra ( \ ), MapPath () restituisce un percorso come se Path era un percorso completo e virtuale.

Se Path non inizia con una barra, MapPath () restituisce un percorso relativo alla directory della richiesta in elaborazione.

Nota: in C #, @ è l'operatore letterale letterale stringa che significa che la stringa deve essere utilizzata " così come " e non essere elaborato per le sequenze di escape.

Note

  1. Server.MapPath (null) e Server.MapPath (" ") produce anche questo effetto .

Altri suggerimenti

Solo per espandere un po 'la risposta di @ splattne:

MapPath (string virtualPath) chiama il seguente:

public string MapPath(string virtualPath)
{
    return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}

MapPath (VirtualPath virtualPath) a sua volta chiama MapPath (VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping) che contiene quanto segue:

//...
if (virtualPath == null)
{
    virtualPath = VirtualPath.Create(".");
}
//...

Quindi, se chiami MapPath (null) o MapPath (" ") , stai effettivamente chiamando MapPath (". ")

1) Server.MapPath (". ") - Restituisce il " Current Physical Directory " del file (ad esempio aspx ) in esecuzione.

Ex. Supponiamo che D:\WebApplications\Collage\Departments

2) Server.MapPath (" .. ") - Restituisce la " Directory principale "

Ex. D: \ applicazioni web \ Collage

3) Server.MapPath (" ~ ") - Restituisce il "Percorso fisico alla radice dell'applicazione"

Ex. D: \ applicazioni web \ Collage

4) Server.MapPath (" / ") - Restituisce il percorso fisico alla radice del nome di dominio

Ex. C: \ Inetpub \ wwwroot

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top