2017-03-03 21 views
1

私はwwwroot/imgフォルダに画像を持っていて、それをサーバ側のコードで使いたいと思っています。ASP.Netコアのwwwroot/imagesから画像を取得

この画像へのパスをコードで取得するにはどうすればよいですか?

コードは次のようである:

Graphics graphics = Graphics.FromImage(path) 

答えて

0
string path = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\images"}"; 
+0

あなたが別のフォルダからあなたの静的なファイルを提供する場合、これは動作しません。さらに悪いことに、 'Directory.GetCurrentDirectory'は、IISのようなサーバにデプロイされたときに[期待どおりに返されません](http://stackoverflow.com/questions/10951599/getting-current-directory-in-net-web-application) 。 –

1

IHostingEnvironmentを注入した後のいずれかでそのWebRootPath又はWebRootFileProviderプロパティを使用するクリーナであろう。コントローラで例えば

:あなたは通常、その特定のファイルのURLを取得するためにUrl.Content("images/foo.png")を使用するビューで

private readonly IHostingEnvironment env; 
public HomeController(IHostingEnvironment env) 
{ 
    this.env = env; 
} 

public IActionResult About(Guid foo) 
{ 
    var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath 
} 

。あなたには、いくつかの理由で物理パスにアクセスする必要があれば、あなたは同じアプローチに従うことができます:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment env 
@{ 
var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath 
} 
関連する問題