I am currently converting a project from .Net Framework into .Net 5.
When I hit one of the endpoints within the new .Net 5 project I get the following exception.
System.InvalidOperationException: 'Misused header name, 'Access-Control-Allow-Origin'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'
The endpoint looks like this, the exception is thrown on the line where I add "Access-Control-Allow-Origin" onto the response content header.
[HttpGet]
[Route("api/Recommendations/GetRecommendations/{id}/{count}")]
public HttpResponseMessage GetRecommendations(int id, int count)
{
var response = new HttpResponseMessage();
response.Content = new StringContent(_recommendationsAPIService.GetRecommendations(id, count));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.Content.Headers.Add("Access-Control-Allow-Origin", "*");
response.Content.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, Referer, Authorization,Sec-Fetch-Mode,User-Agent");
return response;
}
How do I fix this exception?
I am not familiar with who originally wrote the project so is there any reason to add these headers onto the response anyway?