I am trying to send array of object to web Api post ActionResult method. my array of objects looks like this
'[{"TitleFeature":"عنوان یک","ValueFeature":"مقداریک","IdProduct":"1"},{"TitleFeature":"عنوان یک","ValueFeature":"مقداریک","IdProduct":"1"}]'
my function JS looks like this
const uri = '/api/Feature/PostFeatures';
const baseURL = window.location.origin;
var dataFeature = getFeature();
var features = JSON.stringify(dataFeature);
$.ajax({
contentType: "application/json",
dataType: 'json',
type: "POST",
url: (baseURL + uri),
data: features,
});
my Controller.cs is looks like this
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult<Feature>> PostFeatures([FromBody] List<Feature> features)
{
if (features != null)
foreach (var item in features)
{
_context.features.Add(item);
}
await _context.SaveChangesAsync();
return Ok();
}
I use Role Based authorization and this is my role:
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "admin")]
public class FeatureController : ControllerBase
how to fix error 405?
The 405 error means that you have entered the URL but the http request does not match, so if there is no route attribute on your action method, you can try to change the ajax request address to .../api/Feature