尚未配置文件提供程序来处理提供的文件

问题描述 投票:3回答:1

我正在将单页应用程序与.Net core 2.2一起使用。我的startup.cs中有以下内容。

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa - fallback",
                    defaults: new { controller = "CatchAll", action = "Index" });

            });

但是有以下内容

public class CatchAllController : Controller
    {
        public IActionResult Index()
        {
            return File("~/index.html", "text/html");
        }
    }

给我以下错误。

未配置文件提供程序来处理提供的文件

我正在关注以下文章(一个不同之处。文章使用API​​项目。我参加过angular项目)。

https://www.richard-banks.org/2018/11/securing-vue-with-identityserver-part1.html

.net-core asp.net-core-mvc asp.net-core-2.0 asp.net-core-2.1
1个回答
0
投票

我只是在解决这个确切的问题。即使File()方法声称采用“虚拟路径”,但如果您遇到错误,我将永远无法获取它来加载文件。在阅读了许多不同的帖子之后,这就是我最终拼凑而成的内容。

[Route("[controller]")]
public class DocumentationController : ControllerBase
{
    private readonly IHostingEnvironment _env;

    public DocumentationController(IHostingEnvironment env)
    {
        _env = env;
    }

    [HttpGet, Route("apiReference")]
    public IActionResult ApiReference()
    {
        var filePath = Path.Combine(_env.ContentRootPath,
            "Controllers/Documentation/My Document.pdf");
        return PhysicalFile(filePath, "application/pdf");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.