为ASP.NET core 2.0 web api启用CORS

编程入门 行业动态 更新时间:2024-10-24 16:31:34
本文介绍了为ASP.NET core 2.0 web api启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用ASP.Net Core 2.0创建了一个Web API,其中我实现了启用CORS的代码,如下所示:

I've created a Web API using ASP.Net Core 2.0 wherein I've implemented code for enabling CORS as given below:

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("AllowFromAll", builder => builder .WithMethods("GET", "POST") .AllowAnyOrigin() .AllowAnyHeader()); }); ; services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("AllowFromAll"); app.UseMvc(); } } }

通过fiddler调用它来验证响应时,我得到http 401:UnAuthorized错误。 我不确定如果我错过了一些启用CORS的实现。 有什么建议吗? 我尝试过:

When verified for the response by calling it through fiddler, I get http 401: UnAuthorized error. I'm not sure If I missed some implementation to enable CORS. Any suggestions please? What I have tried:

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("AllowFromAll", builder => builder .WithMethods("GET", "POST") .AllowAnyOrigin() .AllowAnyHeader()); }); ; services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("AllowFromAll"); app.UseMvc(); } } }

推荐答案

朋友请参考此网址,我相信你可以如果没有找到你的答案然后只是让我知道我会解决它:在ASP.NET Web API 2中启用跨源请求Microsoft Docs [ ^ ] 步骤1- Hi friend refer this url and i am sure you can find your answer if not then just do let me know i will fix it : Enabling Cross-Origin Requests in ASP.NET Web API 2 | Microsoft Docs[^] Step 1- using System.Web.Http; namespace WebService { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // New code config.EnableCors(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }

步骤2 -

Step 2-

using System.Net.Http; using System.Web.Http; using System.Web.Http.Cors; namespace WebService.Controllers { [EnableCors(origins: "yourclientApplicationRootURL", headers: "*", methods: "*")] public class TestController : ApiController { // Controller methods not shown... } }

更多推荐

为ASP.NET core 2.0 web api启用CORS

本文发布于:2023-11-10 09:34:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:core   NET   ASP   CORS   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!