Zugriff auf xmlhttprequest unter 'https://api.contoso.com/v1/controller/v ... /upleLload' von originllost von origin 'https://www.contoso.com' wurde durch CORS-Richtlinien blockiert: Nein 'Access-control-Allow-Origin' Header ist in der angeforderten Ressource vorhanden. 413 (Anfrage Entity zu groß) < /p>
< /blockquote>
Code: Select all
builder.Services.Configure(options =>
{
options.Limits.MaxRequestBodySize = long.MaxValue;
options.Limits.MaxRequestBufferSize = long.MaxValue;
});
builder.Services.Configure(options =>
{
options.MultipartBodyLengthLimit = 200 * 1024 * 1024; //200 MB
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
//DB connection
//Blob storage connection
//Auth/JWT
//Services/DI
var app = builder.Build();
app.Logger.LogInformation("Starting...");
< /code>
In Produktion: < /p>
app.Logger.LogInformation("Production mode");
app.UseCors(policyBuilder =>
{
policyBuilder.SetIsOriginAllowed((origin) => {
app.Logger.LogInformation("Origin of call: " + origin);
return origin.Equals("https://contoso.com") || origin.Equals("https://www.contoso.com");
})
.AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("App-Desktop");
});
//Allow access by the desktop app to these endpoints.
app.Use(async (context, next) =>
{
if (context.Request.Headers.ContainsKey("App-Desktop"))
{
var path = context.Request.Path.Value?.ToLower() ?? "";
app.Logger.LogInformation("Path of call from desktop app: " + path);
if (path.StartsWith("/v1/blogs"))
context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
}
await next();
});
< /code>
Die betreffende Controller -Aktion: < /p>
[HttpPost("upload")]
[Consumes("multipart/form-data")]
//[RequestSizeLimit(209_715_200)] //200Mib
[DisableRequestSizeLimit]
public async Task UploadBinary(IFormFile file, [FromForm] BinaryRequest binary)
{
_logger.LogInformation("Uploading asset: " + file.FileName);
//...
}
Request starting HTTP/1.1 OPTIONS https://api.contoso.com/v1/controller/upload - - -
Ursprung des Anrufs: https://www.contoso.com < /p>
< /blockquote>
CORS -Richtlinienausführung erfolgreich. https://api.contoso.com/v1/controller/upload - 204 - - 2.1230ms
Die Controller -Aktion wird nie ausgeführt, sodass die Nachricht mit "hochgeladenen" -Meldungen nicht ausgewählt wurde. /> Was könnte der Grund dafür sein?
Code: Select all
builder.Services.Configure(options =>
{
options.Limits.MaxRequestBodySize = long.MaxValue;
options.Limits.MaxRequestBufferSize = long.MaxValue;
options.Limits.MaxRequestHeadersTotalSize = int.MaxValue;
options.Limits.MaxResponseBufferSize = long.MaxValue;
});
builder.Services.Configure(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = long.MaxValue;
options.MultipartHeadersLengthLimit = int.MaxValue;
options.BufferBodyLengthLimit = long.MaxValue;
options.MemoryBufferThreshold = int.MaxValue;
});