默认情况下,Asp.net core 将仅渲染来自wwwroot
目录。
The wwwroot
是一个特殊的目录,用于保存所有静态资源(您的图像/css/js/静态文件,例如您的pdf等)。
因此,将您的 pdf 文件移至wwwroot
目录和链接将起作用。
可以指定另一个目录作为提供其他静态内容的目录。假设您有一个名为MyPdfs
在应用程序根目录中,您可以显式添加此目录作为 StaticFile 源之一。为此,请转到 Startup.cs 并更新配置方法以包含以下代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// Your existing code goes here
app.UseStaticFiles();
// This will add "Libs" as another valid static content location
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"MyPdfs")),
RequestPath = new PathString("/pdfs")
});
}
The PhysicalFileProvider
类定义在Microsoft.Extensions.FileProviders
命名空间。所以你应该在你的代码中添加一个 using 语句Startup.cs
class.
using Microsoft.Extensions.FileProviders;
现在您可以获得一个链接,其中包含href
属性指向/pdfs/yourFileName.pdf
您还需要删除文件名中的空格,将其替换为_
or -
<a href='@Url.Content("~/pdfs/CMT-RPT_User_Guide.pdf.pdf")'>Link </a>