[英]401.2 response when call WEB API from MVC project
I am running a WEB API project locally through Visual studio, on port 49374. 我正在通过Visual Studio在端口49374上本地运行WEB API项目。
I am then running an MVC project locally through VS, on port 57062. 然后,我在57062端口上通过VS在本地运行MVC项目。
I am trying to call an API in my WEB API project (49374), from the MVC project(57062), but am getting a 401.2 response, see below: 我正在尝试从MVC项目(57062)调用我的WEB API项目(49374)中的API,但收到401.2响应,请参见下文:
When I call the API directly from the browser, it works fine. 当我直接从浏览器调用API时,它可以正常工作。
CORS is setup in the Web API Web config as follows: 在Web API Web配置中设置了CORS,如下所示:
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:57062" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Credentials" value="true" />
<!--<add name="Access-Control-Allow-Credential-Header" value="true"/>-->
</customHeaders>
</httpProtocol>
and the project has the following settings on VS: 并且该项目在VS上具有以下设置:
I am out of ideas as to what the problem could be - can anyone suggest anything? 我对可能的问题不了解-有人可以提出任何建议吗?
I have encountered the same probleme. 我遇到了同样的问题。 IIS Express does not seem to use the custom headers in the web.config.
IIS Express似乎没有在web.config中使用自定义标头。
I fixed it by adding in the global.asax.cs : 我通过添加global.asax.cs来修复它:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Context.Request.Headers.AllKeys.Contains("Origin"))
{
Context.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:57062");
Context.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
Context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
if (Context.Request.HttpMethod == "OPTIONS") Context.Response.End();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.