简体   繁体   English

如何在没有控制器的 Razor 页面中找到 wwwroot 的物理路径?

[英]How can I find the physical path of wwwroot, in a Razor page without a controller?

I need to make a file list in a list, which used to be easy.. but not on Asp.Net Core 2 Razor pages.我需要在一个列表中创建一个文件列表,这在过去很容易......但不是在 Asp.Net Core 2 Razor 页面上。 I cannot find a way to get the physical path of "Poems" which is inside "wwwroot".我找不到获取“wwwroot”内“Poems”物理路径的方法。 I found many examples using IHostingEnvironment but always in a controller.我发现了很多使用IHostingEnvironment例子,但总是在控制器中。 But I don't have a controller, don't need one, just the page and code behind..但是我没有控制器,不需要控制器,只有页面和代码后面..

Oh, how I miss WebForms!哦,我多么想念 WebForms!

What is the solution for a Razor Page without a controller?没有控制器的 Razor Page 的解决方案是什么?

Thanks in advance.提前致谢。

You can make it in Razor Page the same way you do it in a controller.您可以像在控制器中那样在 Razor Page 中制作它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Viber.Pages
{
    public class TestModel : PageModel
    {
        private Microsoft.AspNetCore.Hosting.IHostingEnvironment _env;
        public TestModel(Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
        {
            _env = env;
        }

        public void OnGet()
        {
            // use the _env here
        }
    }
}

You can use the contractor of the PageModel to inject the IHostingEnvironment.您可以使用 PageModel 的承包商来注入 IHostingEnvironment。

You can use IWebHostEnvironment class.您可以使用 IWebHostEnvironment 类。 Code block is given below:代码块如下:

private readonly IWebHostEnvironment _webHostEnvironment;

        public HomeController(
            IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

public IActionResult Index()
        {
            string rootPath = _webHostEnvironment.WebRootPath
            return View();
        }     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Razor 页面上创建指向存储在 wwwroot 中的图像的超链接? - How do I create a hyperlink on a Razor page to an image stored in wwwroot? 如何正确地将来自 wwwroot 的图像提供给“页面”子文件夹中的剃刀页面? - How do I properly serve images from the wwwroot to razor pages in a “Page” subfolder? Razor页面如何在DisplayNameFor中找到该字段? - How can Razor page find the field in DisplayNameFor? Asp.net 核心 3.1- 我如何从 Controller 返回到 Razor 页面? - Asp.net Core 3.1- How can I return from Controller to Razor Page? 如何以编程方式设置此 xml 文件的物理路径? - How can I set the physical path of this xml file programmatically? 如何找到Http模块组装的物理路径 - how to find the physical path of Http module Assembly 如何在ASPX页面中指定物理路径? - How to specify physical path in ASPX page? 如何在WebAPI Controller中从物理路径生成绝对路径 - How to generate absolute path from physical path in WebAPI Controller 我如何在Webjob内的Azure托管ASP .NET Core应用程序的wwwroot文件夹中获取图像文件的路径 - How can i get the path to an image file in wwwroot folder of an azure hosted asp .net core app inside a webjob 在MVC6中如何阻止直接访问wwwroot中的文件夹? - In MVC6 how can I block direct access to a folder in wwwroot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM