小编dev*_*ull的帖子

Web API 2 CORS IIS Express调试和无Access-Control-Allow-Origin标头

空的web.api项目,安装Microsoft.aspnet.webapi.cors 5.2.3,添加

config.EnableCors();
Run Code Online (Sandbox Code Playgroud)

到webapiconfig。使控制器和动作

public class HomeController : ApiController
{
    [EnableCors("*" , "*" , "*")]
    public async Task<string> Get()
    {
        return await Task.FromResult("omg");
    }
}
Run Code Online (Sandbox Code Playgroud)

调试应用程序并加载提琴手,并向http:// localhost:61939 / api / Home发出请求

没有CORS标头。web.config包含:

<system.webServer>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Run Code Online (Sandbox Code Playgroud)

我想念什么?为什么这不会在对Get方法的所有请求中插入Access-Control-Allow-Origin标头?

同样,在web.config中定义CORS的答案也不是答案。在某些时候,我将需要添加原始检查,甚至可能需要检查HTTP方法,例如:

if(requestContext.HttpMethod == "POST" && (origin == "https://someplace.com" || origin == "http://localhost"))
Run Code Online (Sandbox Code Playgroud)

cors iis-express asp.net-web-api iis-10

1
推荐指数
1
解决办法
4359
查看次数

标签 统计

asp.net-web-api ×1

cors ×1

iis-10 ×1

iis-express ×1