I have a front-end WebAPI built with Angular and TypeScript that has the following structure.
removeSubset(id: number): ng.IPromise<any> {
return this.$http.post(this.api + '/DeleteStudySubset', id)
.then(this.returnData);
}
returnData = (response: any) => {
return response.data;
};
The corresponding back-end version is structured as follows:
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
public async Task<IHttpActionResult> DeleteStudySubset(int id)
{
await _subsetRepo.DeleteStudySubset(id);
return Ok();
}
Initially, I encountered a URI 404 Error and struggled to find a solution. Eventually, I discovered the parameter binding [FromBody] attribute, which resolved the 404 issue.
Therefore, I updated the back-end API to include the attribute like this:
[HttpPost]
[ResponseType(typeof(IHttpActionResult))]
public async Task<IHttpActionResult> DeleteStudySubset([FromBody]int id)
{
await _subsetRepo.DeleteStudySubset(id);
return Ok();
}
Based on my limited understanding, it seems that [FromBody] instructs the back end to parse the body of the incoming request instead of relying solely on the URL. Is this correct? I may not be articulating my comprehension effectively.
In essence, my main question is whether using the [FromBody] attribute to address the 404 error is considered best practice or merely a temporary fix. I am concerned that it might be masking an underlying configuration issue between my front-end and back-end APIs rather than resolving it effectively.