Topic: Asp.net core vs CORS
Greetings to all! I want to add own handler on POST \GET \PUT requests and it seemed to make it simply enough: Start.cs public void ConfigureServices (IServiceCollection services) {services. AddMvcCore (); services. Configure <MvcOptions> (options => {options. InputFormatters. Add (new MyInputFormatter ());}); services. AddOptions (); services. AddCors (o => o. AddPolicy ("mp", builder => {builder. AllowAnyHeader ().AllowAnyMethod ().AllowAnyOrigin ();}));} MyInputFormater.cs public class MyInputFormater: InputFormatter {static MediaTypeHeaderValue protoMediaType = MediaTypeHeaderValue. Parse ("application/mp-sarbuf"); public override bool CanRead (InputFormatterContext context) {var request = context. HttpContext. Request; MediaTypeHeaderValue requestContentType = null; MediaTypeHeaderValue. TryParse (request. ContentType, out requestContentType); if (requestContentType == null) {return false;} return requestContentType. IsSubsetOf (protoMediaType);} public override Task <InputFormatterResult> ReadRequestBodyAsync (InputFormatterContext context) {if (context == null) throw new ArgumentNullException (nameof (context)); var request = context. HttpContext. Request; var resultObject = new ProtocolIn (); if (Parse (request. Headers, ref resultObject)) {var data = Encoding. UTF8.GetString (ReadFully (context. HttpContext. Request. Body)); resultObject. Body = Convert. FromBase64String (data); return InputFormatterResult. SuccessAsync (resultObject);} return InputFormatterResult. FailureAsync ();} private bool Parse (IHeaderDictionary header, ref ProtocolIn obj) {//... Filling of fields ProtocolIn return true;} private byte [] ReadFully (Stream stream) {byte [] buffer = new byte [32768]; using (var ms = new MemoryStream ()) {while (true) {int read = stream. Read (buffer, 0, buffer. Length); if (read <= 0) return ms. ToArray (); ms. Write (buffer, 0, read);}}}} but sticks into wheels to me interposed CORS with the requests OPTIONS when it checks, and whether it is possible... And its requests with Method = "OPTIONS" are intercepted also by mine MyInputFormater. How more correctly to make that and CORS it is correct the restrictions (while at me they are not present in an example) and did not climb in mine "MyInputFormater"? Now the first request OPTIONS does CORS, CanRead speaks return false