如果资源位置被 UseStaticFiles 更改,Iframe 不会显示资源

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

我想在 Blazor 服务器应用程序中使用

iframe
来显示 PDF 资源。我已经使用
UseStaticFiles
中间件将资源位置提供给
iframe
,但它不起作用。

这是代码片段,其中 test.pdf 已放置在

"C:\temp\CRM\Static\temp\pdf\test.pdf"
:

Program.cs

    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(@"c:\temp\CRM\Static"),
        RequestPath = "/Static",      
    });

page. Razor

    <iframe src="temp/pdf/test.pdf"></iframe>

更新

实际上我的应用程序放置在 c: emp\CRM\Services 目录中,如 builder.Environment.ContentRootPath 所示,PDF 文件放置在 c: emp\CRM\Static emp\pdf 目录中。

app.UseStaticFiles(new StaticFileOptions 
{
     FileProvider = new PhysicalFileProvider(
          Path.Combine(builder.Environment.ContentRootPath, @"..\Static")),
     RequestPath = "/Static"
});

这意味着,我尝试为 iframe 提供资源的可用路径,但它不起作用。看来符号“..\Static”不正确。

更新2

“..”符号现已更正,但它也不起作用:

string sContentRootPath = builder.Environment.ContentRootPath;
int idx = sContentRootPath.LastIndexOf("\\");
idx = sContentRootPath.LastIndexOf("\\", idx - 1);
string sContentRoot = sContentRootPath.Substring(0, idx);

app.UseStaticFiles(new StaticFileOptions
{
     FileProvider = new 
     PhysicalFileProvider(Path.Combine(sContentRoot, "Static")),
     RequestPath = "/Static",

});

谁能告诉我,我错了什么?

iframe blazor-server-side middleware .net-7.0
1个回答
0
投票

现在我找到了我的错误和正确答案。在最后一次更新 2 中,我需要像这样更正 ifarme src 属性:

<iframe src="/Static/temp/pdf/test.pdf"></iframe>

现在可以使用了。

© www.soinside.com 2019 - 2024. All rights reserved.