ngx扩展的pdf-viewer cors cors误差Angular和asp.net core

发布于 2025-02-07 12:04:45 字数 1031 浏览 1 评论 0 原文

我试图使用ASP.NET Core作为后端的Angular中的NGX-Extended-extended-extended-pdf-ciewer查看PDF文件。这是我的模板:

<ngx-extended-pdf-viewer [src]="pdfsrc" 
                        [height]="'95%'"
                        useBrowserLocale="true"
                        [textLayer]="true"
                        [showHandToolButton]="true"
                        >
                    </ngx-extended-pdf-viewer>

在我的component.ts

 pdfsrc: any;
this.pdfsrc = this.children.studyPath;
       this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfsrc)

cors策略中,在ASP.NET核心中,

     app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:4200"));

中获得此错误访问

我在'https:// localhost:5001/childstudyreportfolder/7a6c78dd-aaa3a-46db-bbea-bbea-37a45678484d1a 。 pdf'从ouncon'https:// localhost:4200'已被CORS策略阻止:在请求的资源上没有“访问conscept-control-allow-origin”标头。如果不透明的响应满足您的需求,请将请求模式设置为“无核”,以通过禁用CORS来获取资源。

I am trying to view pdf file using ngx-extended-pdf-viewer in angular with ASP.net Core as backend. This is my template:

<ngx-extended-pdf-viewer [src]="pdfsrc" 
                        [height]="'95%'"
                        useBrowserLocale="true"
                        [textLayer]="true"
                        [showHandToolButton]="true"
                        >
                    </ngx-extended-pdf-viewer>

in my component.ts

 pdfsrc: any;
this.pdfsrc = this.children.studyPath;
       this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfsrc)

CORS policy in ASP.Net Core

     app.UseCors(policy => policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:4200"));

I am getting this error Access to fetch at

'https://localhost:5001/ChildStudyReportFolder/7a6c78dd-aa3a-46db-bbea-37a456784d1a.pdf' from origin 'https://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-02-14 12:04:45

问题是app.usestaticfiles();
在启动cs中使用app.usecors()之前放置;

在app.usecors()之后放置app.usestaticfiles()之后;
我清除了所有缓存数据,并且有效。谢谢

the issue was that app.UseStaticFiles();
was placed before use app.UseCors() in the startup.cs;

After placing app.UseStaticFiles() after app.UseCors();
I cleared all cache data and it works. Thanks

孤星 2025-02-14 12:04:45

如果您可以显示更多详细信息,可能会有所帮助
在ASP.NET Core中启用CORS,您应该按以下设置设置:

public void ConfigureServices(IServiceCollection services)
    {
        ........
        services.AddCors(options =>
        {
            options.AddPolicy(name: MyAllowSpecificOrigins,
                              policy =>
                              {
                                  policy.WithOrigins("......");
                              });
        });
        ......
    }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        ......
        app.UseRouting();
        ......

        app.UseCors(MyAllowSpecificOrigins);

        ......

        app.UseAuthorization();

        ......
    }

注意中间件的顺序, app.usecors(myallowspecificorigins); 应该在 app.userouting(); 后面。在 app.useuthentication();

添加 [enablecors]

外部文档相关:

It may help if you could show more details,
Enable CORS in ASP.NET Core,you should set as below :

public void ConfigureServices(IServiceCollection services)
    {
        ........
        services.AddCors(options =>
        {
            options.AddPolicy(name: MyAllowSpecificOrigins,
                              policy =>
                              {
                                  policy.WithOrigins("......");
                              });
        });
        ......
    }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        ......
        app.UseRouting();
        ......

        app.UseCors(MyAllowSpecificOrigins);

        ......

        app.UseAuthorization();

        ......
    }

notice the order of the middleware,app.UseCors(MyAllowSpecificOrigins);should behind app.UseRouting();and beforeapp.UseAuthentication();

add [EnableCors] on your controller

The offcial document related:
https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文