【问题标题】:Path to static file under wwwroot in Razor PagesRazor Pages 中 wwwroot 下静态文件的路径
【发布时间】:2020-02-21 23:09:29
【问题描述】:

这三种获取wwwroot下图片路径的方法有什么区别?在我的情况下似乎所有工作都相同,但想了解它们之间是否存在任何其他具体情况的差异或使用一种方法的好处。

我使用此路径随后将图像加载到Bitmap MyBitmap 变量中以进行进一步处理。无论最终部署到 Windows、Linux 还是容器,都希望它具有环境弹性;本地或云端。

在 ASP.NET Core 3.0 中使用 Razor 页面。

public class QRCodeModel : PageModel
{
    private readonly IHostEnvironment hostingEnvironment;

    public QRCodeModel(IHostEnvironment environment)
    {
        this.hostingEnvironment = environment;
    }

    public void OnGet()
    {
        string TempPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "Image1.png");
        string TempPath1 = Path.Combine(Environment.CurrentDirectory, "wwwroot", "img", "Image1.png");
        string TempPath2 = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot", "img", "Image1.png");
    }
}

【问题讨论】:

    标签: asp.net-core razor-pages


    【解决方案1】:

    还有一个选择:

    string TempPath4 = Path.Combine(hostingEnvironment.WebRootPath, "img", "Image1.png");
    

    WebRootPath 返回 wwwroot 文件夹的路径。

    建议使用前两个选项,因为它们可能不会返回您想要的位置:Best way to get application folder path

    【讨论】:

    猜你喜欢
    • 2022-11-04
    • 2022-07-14
    • 2019-07-02
    • 2023-03-18
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2020-01-20
    • 2021-08-29
    相关资源
    最近更新 更多