Web API 2を既存のVB aspx Webフォームプロジェクトに追加しました。私は標準のWeb APIプロジェクトのようにWebApiConfigを持つapp_startフォルダを持っていないので、ルーティングはグローバルasax application_startに入りました。私は、しかし、私は私が受け取るjqueryのAJAXを通じて自分のWeb APIへの呼び出しを行っているHTMLページを実行しようとするたびにCORSaspx web api 2 hybridにcorsを追加する
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
RouteTable.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
Dim cors = New EnableCorsAttribute("*", "*", "*")
GlobalConfiguration.Configuration.EnableCors(cors)
End Sub
を有効にしようとしたaddがナゲットパッケージマネージャからCORSをダウンロードしました。
Cross-Origin Request Blocked: The Same Origin Policy
disallows reading the remote resource at https://xxxxx/specialdev/api/WSFobOrigin.
(Reason: CORS header 'Access-Control-Allow-Origin' missing)
私は、私が各コントローラーにも同様にそれを追加しようとしています。ここで
Public Class WSFobOriginController
Inherits ApiController
<EnableCors("*", "*", "*")>
<HttpGet>
<CustomAuthentication>
<Authorize(Roles:="WebService")>
Public Function logon() As IHttpActionResult
Return Ok("successfully loggon on")
End Function
AJAX呼び出し(私はクロスドメインでとせずに、それを試してみました:真)さ
this.logon = function() {
$('#signin').prop('disabled', true);
$.ajax({
url: "https://xxxxxxxx.dir.ad.dla.mil/specialdev/api/WSFobOrigin",
type: "GET",
datatype: "json",
crossDomain: true,
beforeSend: function (xhr) {
$('#logonSpinner').show();
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.userName() + ":" + self.password()));
},
success: function (data) {
self.loggedon(true);
},
error: function (xhr, status, error) {
$('#signin').prop('disabled', false);
$('#logonSpinner').hide();
$('#logonError').show();
self.logOnErrorMessage("Status: " + xhr.status + " Message: " + xhr.statusText)
}
});
}
はちょうど私には少し奇妙であるもう一つの事に気づきました。私はローカル(ビジュアルスタジオを介して)Web APIを実行し、ローカルURLに私のクライアントのjqueryのajax呼び出しを変更するときに動作します。
URL Protocol Method Result Type Received Taken Initiator Wait Start Request Response Cache read Gap
http://localhost:52851/api/WSFobOrigin HTTP OPTIONS 200 420 B 31 ms CORS Preflight 0 16 0 15 0 203
と
URL Protocol Method Result Type Received Taken Initiator Wait Start Request Response Cache read Gap
http://localhost:52851/api/WSFobOrigin HTTP GET 200 application/json 447 B 218 ms XMLHttpRequest 16 15 203 0 0 0
が、私はプリフライトは中止され、タイプは、もはやOPTIONSを言わない、それはいくつかの他の記事
URL Protocol Method Result Type Received Taken Initiator Wait Start Request Response Cache read Gap
https://xxxxxxx.dir.ad.dla.mil/specialdev/api/WSFobOrigin HTTPS (Aborted) 0 B 47 ms CORS Preflight 0 47 0 0 0 796
nullの実際のサーバーを指すようにクライアントを変更私が試したフィルタを追加することを提案しましたが、どちらもうまくいかないようです。
I mportsのSystem.Web.Http.Filters
Public Class AllowCors
Inherits ActionFilterAttribute
Public Overrides Sub OnActionExecuted(actionExecutedContext As HttpActionExecutedContext)
If actionExecutedContext Is Nothing Then
Throw New ArgumentNullException("actionExecutedContext")
Else
actionExecutedContext.Response.Headers.Remove("Access-Control-Allow-Origin")
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*")
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type")
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Methods", "GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS")
End If
MyBase.OnActionExecuted(actionExecutedContext)
End Sub
End Class
とallowcors
<AllowCors>
<EnableCors("*", "*", "*")>
<HttpGet>
<CustomAuthentication>
<Authorize(Roles:="WebService")>
Public Function logon() As IHttpActionResult
Return Ok("successfully loggon on")
End Function
まだ運
status: 404
Method: OPTIONS
Request Headers: Host: xxxxxx.dir.ad.dla.mil
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: null
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Connection: keep-alive
Response Headers: Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
X-Frame-Options: SAMEORIGIN
Date: Wed, 23 Mar 2016 16:53:06 GMT
Content-Length: 1245
をこの設定を追加しようとすることができます構造体の 'App_code'で' Startup.cs'を見つけることができますか?あなたはweb api2 – Webruster
を使用していると言いましたので、これはどうやってダウンしましたか?私たちは既存のaspx Webフォームアプリケーションを持っています。そして、私はnuに入り、事実の後にWeb APIパッケージに追加しました。だから私は通常あなたのような典型的なapp_startフォルダを持っていないのですが、bundle_config、route_configなどがあります。代わりに、ルーティングなどは、application_start(私のポストの最初のコードブロックを参照)の下でグローバルasaxに入ります。それを悪臭にする他のものは、VBのアプリケーションです、それはVBのを使用してWeb APIの例を見つけることは困難でした。 –
あなたのwebconfigを表示することができますし、この方法でcorsを有効にしようとすることもできます 'var context = HttpContext.Current; \t \t var response = context.Response; \t \t \t \t // CORSを有効 \t \t response.AddHeader( "アクセス制御 - 許可 - 起源"、 "*"); \t \t Response.AddHeader( "X-Frame-Options"、 "ALLOW-FROM *"); in Application_Start – Webruster