我想在我的.net核心2.0项目中添加一个PDF文件,它在我的localhost上使用IIS Express运行,我已经将pdf文件添加到我的项目文件中,它显示在解决方案资源管理器中,我添加了我的.cshtml中的相应链接如下:
<a href=@Url.Content("~/CMT-RPT User Guide.pdf")>Link to guide if it does not show</a><br/>
但是在我运行之后,它无法显示在我的网站上,有什么问题? 以下是我的解决方案资源管理器和我点击该链接的网站的屏幕截图。
答案 0 :(得分:3)
默认情况下,Asp.net核心只会从wwwroot
目录呈现静态内容。
wwwroot
是一个特殊目录,用于保存所有静态资源(您的图像/ css / js /静态文件,如pdf等)。
因此,将您的pdf文件移至wwwroot
目录,该链接将起作用。
可以将另一个目录指定为用于提供其他静态内容的目录。假设您在app根目录中有一个名为MyPdfs
的目录,您可以将此目录显式添加为StaticFile源之一。为此,请转到Startup.cs并更新Configure方法以获得以下代码
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")
});
}
PhysicalFileProvider
类在Microsoft.Extensions.FileProviders
命名空间中定义。因此,您应该在Startup.cs
类中添加using语句。
using Microsoft.Extensions.FileProviders;
现在,您可以拥有一个链接,其href
属性指向/pdfs/yourFileName.pdf
您还需要删除文件名中的空格,将其替换为_
或-
<a href='@Url.Content("~/pdfs/CMT-RPT_User_Guide.pdf.pdf")'>Link </a>