C# AddCors不显示访问控制允许源ASP.NET的任何标题
我是ASP.NET的新手,第二次尝试在.NET中编码,所以请耐心等待我,并感谢您对第一个项目的帮助。下面的项目试图创建一个.NET API,该API将仅为React应用程序获取JSON以显示数据网格和分页数据 正如标题所述,我没有在开发人员工具的标题部分看到访问控制允许源代码-请参阅附加的屏幕截图 正如您在前面的屏幕截图中所看到的,它按预期工作,只是标题没有显示任何关于访问控制允许来源的内容,这导致我的react应用程序在我将JSON放入文件并让react应用程序检索本地.JSON文件时抛出错误“SyntaxError:意外输入结束”,一切都按预期进行,这证实了问题在于没有接收到头中的“Access Control Allow Origin”。下面的代码是我的Startup.cs文件,它使用MySql检索数据C# AddCors不显示访问控制允许源ASP.NET的任何标题,c#,json,asp.net-web-api,cors,C#,Json,Asp.net Web Api,Cors,我是ASP.NET的新手,第二次尝试在.NET中编码,所以请耐心等待我,并感谢您对第一个项目的帮助。下面的项目试图创建一个.NET API,该API将仅为React应用程序获取JSON以显示数据网格和分页数据 正如标题所述,我没有在开发人员工具的标题部分看到访问控制允许源代码-请参阅附加的屏幕截图 正如您在前面的屏幕截图中所看到的,它按预期工作,只是标题没有显示任何关于访问控制允许来源的内容,这导致我的react应用程序在我将JSON放入文件并让react应用程序检索本地.JSON文件时抛出错
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace v2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// 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("AllowSpecific", p => p.WithOrigins("http://localhost:3006")
.WithMethods("GET")
.WithHeaders("name")));
services.AddMvc(option => option.EnableEndpointRouting = false);
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
//MySql
services.AddTransient<MySqlDatabase>(_ => new MySqlDatabase("server=xyz.com; database=someDB; uid=someUser; pwd=somePwd; port=somePort;"));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.EnvironmentName == "dev")
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseCors(builder =>
builder.WithOrigins("http://localhost:3006")
.AllowAnyHeader()
);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "OrderItem",
template: "OrderItem/{div?}/{yr?}",
defaults: new { controller = "OrderItem", action = "Index" });
});
}
}
}
我是否必须在_布局和cshtml页面中执行类似的操作
还有一点,我将无法尝试您的建议,直到我在周一返回工作,但像往常一样,提前感谢您,并感谢您的帮助。结果表明,我需要添加一个属性类,并将其提供给整个控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http.Filters;
namespace v2
{
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:3006");
base.OnActionExecuted(actionExecutedContext);
}
}
}
然后将其添加到OrderItemController:
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using dto = v2.Models;
using mFilter = v2.AllowCrossSiteJsonAttribute;
namespace v2.Controllers
{
[AllowCrossSiteJson]
虽然我确实看到了一篇类似的帖子,上面有这样的答案,但直到我补充说:
using mFilter = v2.AllowCrossSiteJsonAttribute;
通过添加以下内容,使其可用于整个控制器:
[AllowCrossSiteJson]
在名称空间下方和控制器类上方
抱歉,没有屏幕截图,我从工作中给自己发送了一封带有屏幕截图的电子邮件,但系统一定阻止了它
像往常一样,提前感谢@KirkLarkin指出,仅仅查看localhost:5001中的标题并不能证明它没有响应我想要查看的标题,这导致我从React应用程序的localhost:3006透视图中查看标题。结果表明我需要添加一个属性类,另外,整个控制器都可以使用它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http.Filters;
namespace v2
{
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:3006");
base.OnActionExecuted(actionExecutedContext);
}
}
}
然后将其添加到OrderItemController:
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using dto = v2.Models;
using mFilter = v2.AllowCrossSiteJsonAttribute;
namespace v2.Controllers
{
[AllowCrossSiteJson]
虽然我确实看到了一篇类似的帖子,上面有这样的答案,但直到我补充说:
using mFilter = v2.AllowCrossSiteJsonAttribute;
通过添加以下内容,使其可用于整个控制器:
[AllowCrossSiteJson]
在名称空间下方和控制器类上方
抱歉,没有屏幕截图,我从工作中给自己发送了一封带有屏幕截图的电子邮件,但系统一定阻止了它
像往常一样,提前感谢@KirkLarkin指出,仅仅从localhost:5001查看标题并不能证明它没有响应我想要查看的标题,这让我从React应用程序的localhost:3006透视图查看标题。屏幕截图不能证明任何事情。当你像那样在浏览器中加载URL时,它不是一个跨源请求,因此不会有跨源标题。感谢你回复@KirkLarkin,但当我在localhost:3006上查看react应用程序中的标题时,它也显示了一个类似的标题。屏幕截图不能证明任何事情。当你像那样在浏览器中加载URL时,它不是一个跨源请求,因此不会有跨源标题。感谢你回复@KirkLarkin,但是当我在localhost:3006上查看react应用程序中的标题时,它也显示了一个类似的标题