2017-04-10 4 views
0

は私が 強制的にCreatedAtRouteを生成する場所をhttpsから開始しますか?

{ ... "location": "http://here_is_right_url_except_http", "status": "201", ... } 

を得る。しかし、ロケーションヘッダー内のURLがhttpsである必要がありそれに応答して、私のAPI

curl -X POST --header 'Content-Type: application/json' 
--header 'Accept: application/json' 
--header 'Authorization: Bearer some_token' -d '{ some_data }' 
'https://here_is_url' 

にPOSTを実行します。

着信要求ヒットバランサはhttpsで、要求されたHTTPはhttpとして送信されます。

答えて

0

この問題を解決するには2つの方法があります。

1)Startup.csでUrlHelperが

public class HttpsUrlHelper : UrlHelper { 
     public HttpsUrlHelper(ActionContext actionContext) 
    : base(actionContext) { 
} 

protected override string GenerateUrl(string protocol, string host, VirtualPathData pathData, string fragment) { 
     return base.GenerateUrl("https", host, pathData, fragment); 
    } 
} 

public class ForcedHttpsUrlHelperFactory : IUrlHelperFactory { 
    public IUrlHelper GetUrlHelper(ActionContext context) { 
     return new HttpsUrlHelper(context); 
    } 
} 

)それ

services.AddSingleton<IUrlHelperFactory, ForcedHttpsUrlHelperFactory>(); 

2を登録するアクション結果のための新しいクラスを作成する必要がありますオーバーライドします。また、HttpsCreatedAtRouteResultのインスタンスを返す独自のCreatedAtRoute関数を実装する必要があります。

public class HttpsCreatedAtRouteResult : CreatedAtRouteResult { 
    public HttpsCreatedAtRouteResult(object routeValues, object value) 
     : base(routeValues, value) { 
    } 

    public HttpsCreatedAtRouteResult(string routeName, object routeValues, object value) 
     : base(routeName, routeValues, value) { 
    } 

    public override void OnFormatting(ActionContext context) { 
     base.OnFormatting(context); 
     var url = context.HttpContext.Response.Headers[HeaderNames.Location]; 

     // do with url whatever you need 

     context.HttpContext.Response.Headers[HeaderNames.Location] = url; 
    } 
} 
関連する問題