2017-08-04 77 views
2

我有一個VisualStudio17無服務器應用程序項目,並使用.net核心Web Api。解析.net核心1.0中的AWS SNS通知

我想確認我的SNS訂閱,但是我有一個問題,AWS發送POST請求,頭部content-type設置爲text/plain; charset=UTF-8,而body是JSON。

下面是他們的documentation爲例請求:

POST/HTTP/1.1 
x-amz-sns-message-type: Notification 
x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe 
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic 
x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55 
Content-Length: 761 
Content-Type: text/plain; charset=UTF-8 
Host: ec2-50-17-44-49.compute-1.amazonaws.com 
Connection: Keep-Alive 
User-Agent: Amazon Simple Notification Service Agent 

{ 
    "Type" : "Notification", 
    "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe", 
    "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", 
    "Subject" : "test", 
    "Message" : "test message", 
    "Timestamp" : "2012-04-25T21:49:25.719Z", 
    "SignatureVersion" : "1", 
    "Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=", 
    "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem", 
    "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55" 
} 

內容類型:文本,身體JSON。這使得解析相當困難,和一個簡單的

public void Post([FromBody] string t) // or dynamic t for the matter

不工作,拋出Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter'.例外。

我錯過了什麼嗎?他們爲什麼要這樣做,我該如何處理這個問題?

回答

1

我使它工作正如我在this中所述,通過將text/plain添加到JsonInputFormatter應格式化的格式中。

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(config => 
    { 
     foreach (var formatter in config.InputFormatters) 
     { 
      if (formatter.GetType() == typeof(JsonInputFormatter)) 
       ((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
         MediaTypeHeaderValue.Parse("text/plain")); 
     } 
    }); 
    ... 
}