.net webAPI跨域问题解决方案

最近在创建自己的个人博客网站,中途遇到的一些问题,记录一下。
用的.net 5.0 webAPI 用到接口会有跨域问题。在网上百度的都是在StartUp类的ConfigureServices方法中添加如下代码:

services.AddCors(options =>
options.AddPolicy("cors",
p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials()));

但是Cors配置不能同时启用 AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()

否则报错 System.InvalidOperationException
HResult=0x80131509
Message=The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the CORS policy by listing individual origins if credentials needs to be supported.
Source=Microsoft.AspNetCore.Cors
所以修改成:

  services.AddCors(options =>
        {
            options.AddPolicy("cors", builder =>
            {
                builder.WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
                //.AllowCredentials()//指定处理cookie
            .AllowAnyOrigin(); //允许任何来源的主机访问
            });
        });

在Startup.Configure中,添加配置

注意: app.UseCors(“cors”); 要写在app.UseAuthorization(); 后面,否则会报错。

 		app.UseRouting();
        app.UseAuthorization();
        app.UseAuthentication();
        //配置Cors
        app.UseCors("cors");
        app.UseEndpoints(endpoints =>
           {
               endpoints.MapControllers();
           });

这些配置完成就能解决跨域问题了,欢迎大家指出错误或评论区讨论问题

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 要解决Web API跨域问题,可以通过在Web.Config中的system.webServer节点下增加跨域配置来实现。具体的配置如下所示: ``` <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="AuthToken, Authorization, Origin, Content-Type, Accept, X-Requested-With" /> <add name="Access-Control-Allow-Methods" value="GET,HEAD,POST,OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer> ``` 另外,还可以在代码中使用EnableCorsAttribute来启用跨域请求。例如,在EnableCrossSiteRequest方法中添加以下代码: ``` private static void EnableCrossSiteRequest(HttpConfiguration config) { var cors = new EnableCorsAttribute( origins: "*", headers: "*", methods: "*" ); config.EnableCors(cors); } ``` 这样就可以解决Web API跨域问题了。\[1\]\[2\] #### 引用[.reference_title] - *1* [ASP.NET WebAPI解决跨域问题](https://blog.csdn.net/hbzhlt/article/details/122221683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [关于 Asp.Net Web Api 跨域问题解决方案](https://blog.csdn.net/squallonline8708/article/details/122728285)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小小陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值