server.mappath (“.”), server.mappath (“~”), server.mappath (@“”), server.mappath (“/”). 차이점은 무엇입니까?

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

문제

누구든지 그 차이를 설명 할 수 있습니다 Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\") 그리고 Server.MapPath("/")?

도움이 되었습니까?

해결책

Server.mappath 맵에 대한 상대 또는 가상 경로를 지정합니다 물리적 디렉토리로.

  • Server.MapPath(".")1 실행중인 파일 (예 : ASPX)의 현재 물리 디렉토리를 반환합니다.
  • Server.MapPath("..") 부모 디렉토리를 반환합니다
  • Server.MapPath("~") 물리적 경로를 응용 프로그램의 루트로 반환합니다.
  • Server.MapPath("/") 물리적 경로를 도메인 이름의 루트로 반환합니다 (반드시 응용 프로그램의 루트와 동일 할 필요는 없습니다).

An example:

웹 사이트 응용 프로그램을 지적했다고 가정 해 봅시다 (http://www.example.com/) 에게

C:\Inetpub\wwwroot

상점 애플리케이션 (IIS의 가상 디렉토리로 서브 웹, 응용 프로그램으로 표시)을 설치했습니다.

D:\WebApps\shop

예를 들어, 전화하는 경우 Server.MapPath() 다음 요청에서 :

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

그 다음에:

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

경로가 전방 슬래시로 시작하는 경우 (/) 또는 후진 슬래시 (\), MapPath() 경로가 가상의 가상 경로 인 것처럼 경로를 반환합니다.

경로가 슬래시로 시작하지 않으면 MapPath() 처리중인 요청의 디렉토리에 대한 경로를 반환합니다.

참고 : C#에서 @ 구두 문자열 연산자는 문자열이 "있는 그대로"사용되어야하며 탈출 시퀀스에 대해 처리되지 않음을 의미합니다.

각주

  1. Server.MapPath(null) 그리고 Server.MapPath("") ~ 할 것이다 이 효과도 생성합니다.

다른 팁

@splattne의 답변을 조금 확장하려면 :

MapPath(string virtualPath) 다음을 호출합니다.

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

MapPath(VirtualPath virtualPath) 차례로 전화 MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping) 다음은 다음을 포함합니다.

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

그래서 당신이 전화하면 MapPath(null) 또는 MapPath(""), 당신은 효과적으로 전화를 걸고 있습니다 MapPath(".")

1) Server.MapPath(".") - 파일의 "현재 물리 디렉토리"를 반환합니다 (예 : aspx) 실행 중.

전. 가정하다 D:\WebApplications\Collage\Departments

2) Server.MapPath("..") - "부모 디렉토리"를 반환합니다.

전. D:\WebApplications\Collage

3) Server.MapPath("~") - "물리적 경로를 응용 프로그램의 근본으로 반환합니다"

전. D:\WebApplications\Collage

4) Server.MapPath("/") - 물리적 경로를 도메인 이름의 루트로 반환합니다.

전. C:\Inetpub\wwwroot

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top