<small id='z3hJI'></small><noframes id='z3hJI'>

    1. <tfoot id='z3hJI'></tfoot>
      <i id='z3hJI'><tr id='z3hJI'><dt id='z3hJI'><q id='z3hJI'><span id='z3hJI'><b id='z3hJI'><form id='z3hJI'><ins id='z3hJI'></ins><ul id='z3hJI'></ul><sub id='z3hJI'></sub></form><legend id='z3hJI'></legend><bdo id='z3hJI'><pre id='z3hJI'><center id='z3hJI'></center></pre></bdo></b><th id='z3hJI'></th></span></q></dt></tr></i><div id='z3hJI'><tfoot id='z3hJI'></tfoot><dl id='z3hJI'><fieldset id='z3hJI'></fieldset></dl></div>
        <bdo id='z3hJI'></bdo><ul id='z3hJI'></ul>

      <legend id='z3hJI'><style id='z3hJI'><dir id='z3hJI'><q id='z3hJI'></q></dir></style></legend>

      1. ASP.NET 5:响应中的访问控制允许来源

        ASP.NET 5: Access-Control-Allow-Origin in response(ASP.NET 5:响应中的访问控制允许来源)

            <legend id='v2h9R'><style id='v2h9R'><dir id='v2h9R'><q id='v2h9R'></q></dir></style></legend>

            <tfoot id='v2h9R'></tfoot>

            <small id='v2h9R'></small><noframes id='v2h9R'>

              • <i id='v2h9R'><tr id='v2h9R'><dt id='v2h9R'><q id='v2h9R'><span id='v2h9R'><b id='v2h9R'><form id='v2h9R'><ins id='v2h9R'></ins><ul id='v2h9R'></ul><sub id='v2h9R'></sub></form><legend id='v2h9R'></legend><bdo id='v2h9R'><pre id='v2h9R'><center id='v2h9R'></center></pre></bdo></b><th id='v2h9R'></th></span></q></dt></tr></i><div id='v2h9R'><tfoot id='v2h9R'></tfoot><dl id='v2h9R'><fieldset id='v2h9R'></fieldset></dl></div>
                  <tbody id='v2h9R'></tbody>

                  <bdo id='v2h9R'></bdo><ul id='v2h9R'></ul>
                  本文介绍了ASP.NET 5:响应中的访问控制允许来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  据我了解,当相应启用 CORS 时,响应模型应包含以下标头信息(前提是我要允许所有内容):

                  From what I understand, when enabled CORS accordingly, the response model should include the following header information (provided that I want to allow everything):

                  Access-Control-Allow-Origin: *
                  Access-Control-Allow-Method: *
                  Access-Control-Allow-Header: *
                  

                  Startup 中启用它:

                  public void ConfigureServices(IServiceCollection services)
                  {
                      //...
                      services.AddCors();
                      services.ConfigureCors(options => 
                      {
                          options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
                      });
                      //...
                  }
                  
                  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                  {
                      //...
                      app.UseCors("AllowAll");
                      //...
                  }
                  

                  问题是这些标头都没有返回,我在尝试从 API 请求时收到以下错误:

                  The problem is that none of these headers are returned and I get the following error when trying to request from the API:

                  对预检请求的响应未通过访问控制检查:否请求中存在Access-Control-Allow-Origin"标头资源.Origin 'http://localhost' 因此不允许访问.

                  Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

                  推荐答案

                  确保在 Startup.Configure 中的 app.UseMvc 之前添加 app.UseCors 方法,因为你需要在 MVC 中间件之前应用 CORS 中间件.

                  Make sure you add app.UseCors before app.UseMvc in your Startup.Configure method, because you need the CORS middleware to be applied before the MVC middleware.

                  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                  {
                      ...
                  
                      //Add CORS middleware before MVC
                      app.UseCors("AllowAll");
                  
                      app.UseMvc(...);
                  }
                  

                  否则请求将在应用 CORS 中间件之前完成.这是因为 UseMvc 调用 UseRouter最终添加了 RouterMiddleware,并且此中间件仅在未找到请求的路由处理程序时执行下一个配置的中间件.

                  Otherwise the request will be finished before the CORS middleware is applied. This is because UseMvc calls UseRouter which ends up adding the RouterMiddleware, and this middleware only executes the next configured middleware when a route handler wasn't found for the request.

                  这篇关于ASP.NET 5:响应中的访问控制允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                    <small id='dBzUT'></small><noframes id='dBzUT'>

                      <bdo id='dBzUT'></bdo><ul id='dBzUT'></ul>
                      <tfoot id='dBzUT'></tfoot>

                          <tbody id='dBzUT'></tbody>

                            <legend id='dBzUT'><style id='dBzUT'><dir id='dBzUT'><q id='dBzUT'></q></dir></style></legend>
                          1. <i id='dBzUT'><tr id='dBzUT'><dt id='dBzUT'><q id='dBzUT'><span id='dBzUT'><b id='dBzUT'><form id='dBzUT'><ins id='dBzUT'></ins><ul id='dBzUT'></ul><sub id='dBzUT'></sub></form><legend id='dBzUT'></legend><bdo id='dBzUT'><pre id='dBzUT'><center id='dBzUT'></center></pre></bdo></b><th id='dBzUT'></th></span></q></dt></tr></i><div id='dBzUT'><tfoot id='dBzUT'></tfoot><dl id='dBzUT'><fieldset id='dBzUT'></fieldset></dl></div>