Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"\")、Server.MapPath("/")。違いは何ですか? 質問する

Server.MapPath(

Server.MapPath(".")、、およびServer.MapPath("~")の違いを説明できる人はいますか?Server.MapPath(@"\")Server.MapPath("/")

ベストアンサー1

Server.MapPath は、物理ディレクトリにマップする相対パスまたは仮想パスを指定します。

  • Server.MapPath(".")1は実行中のファイル(例:aspx)の現在の物理ディレクトリを返します。
  • Server.MapPath("..")親ディレクトリを返します
  • Server.MapPath("~")アプリケーションのルートへの物理パスを返します
  • Server.MapPath("/")ドメイン名のルートへの物理パスを返します(アプリケーションのルートと同じである必要はありません)

例:

ウェブサイトアプリケーション(http://www.example.com/)を

C:\Inetpub\wwwroot

ショップアプリケーション(IISの仮想ディレクトリとしてサブWeb、アプリケーションとしてマーク)をインストールしました

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

Path がスラッシュ ( /) またはバックスラッシュ ( \) で始まる場合、 はMapPath()Path が完全な仮想パスであるかのようにパスを返します。

Path がスラッシュで始まっていない場合、MapPath()処理中のリクエストのディレクトリを基準とした相対パスが返されます。

注: C# では、@文字列を「そのまま」使用し、エスケープ シーケンスとして処理しないことを意味する、逐語的なリテラル文字列演算子です。

脚注

  1. Server.MapPath(null)そしてServer.MapPath("")この効果も生み出す

おすすめ記事