Web API 2プロジェクトが別のドメインからアクセスする必要があります。 GETリクエストは正常に動作しますが、POSTはフライト前のリクエストで失敗します。 OPTIONSが正しく返るように要求することはできません。Web API 2 CORSプレフライトオプションリクエストが504エラーで失敗する
NuGetパッケージMicrosoft.AspNet.WebApi.Corsをプロジェクトに追加しました。 WebApiConfig.csで
私が呼ん:私が追加したglobal.asax.csで
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
<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>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
:
public static void Register(HttpConfiguration config)
{
config.EnableCors();
私のWeb.configファイルには、これらの値が含まれてい
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
フィドラーで私は
のOPTIONS/API /アプリ/コントローラHTTP/1.1
が、応答がある
HTTP/1.1 504フィドラー - 受信失敗AngularJSが要求を行う
、それを次のヘッダーがあります。
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:4104
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:4104/index.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Chrome p同じヘッダーを持つAdvanced REST Client用のluginも失敗します。しかし、ヘッダAccess-Control-Request-Method: POST
を削除すると、OPTIONSリクエストから返される200の応答が返されます。
更新:
クローム高度なRESTクライアントからの要求を行う際
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
にGlobal.asax.csコードを変更した後、私は200応答コードを取得します。しかし、私はまだWeb APIを呼び出す角度コードと同じタイムアウトが表示されます。これはApplication_BeginRequest GET
をログに記録しますGET
要求の場合
protected void Application_BeginRequest(object sender, EventArgs e)
{
log.Debug("Application_BeginRequest " + HttpContext.Current.Request.HttpMethod);
:私はログを含めるようにApplication_BeginRequest
の最初の行を変更した
$http({
method: 'POST',
url: 'http://www.test.com/api/app/controller',
data: postdata,
headers: {
'Content-Type': 'application/json'
}
})
.then(function(response) {
// Do stuff
}, function() {
// Show error
})
.finally(function() {
// Cancel loading indicator
});
:
これはAngularJsコードです。 AngularJSからOPTIONS
リクエストが行われたときには何も記録されません。コードがApplication_BeginRequest
に決して届かないようです。 Chrome Advanced REST APIを使用して同じマシンから通話を発信すると、ログに記録され(正しく動作します)
ウェブAPIにOwinパイプラインを使用していますか? – jpgrassi
いいえ私はそうではありません。ありがとう。 – psych
良い問題!、この行( ' ')は 'web.config'で非常に重要です。あなたは正しく、tnx –
AliDK