Can't access to my method delete of my api rest

风流意气都作罢 提交于 2020-02-25 05:55:25

问题


I'm not able to access to my delete method of my api rest.

If i write the method like this it work:

[Route("api/Document/{documentId:int}")]
    [HttpDelete]
    public IHttpActionResult Delete([FromUri]int documentId,[FromBody] int [] documentsId)
    {
        try
        {
            documentCtrl = documentCtrl ?? new DocumentCtrl();
            return Ok(documentCtrl.Delete(documentsId));
        }
        catch (DocumentNotFoundException)
        {
            return NotFound();
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

It works, but if i put:

[Route("api/Document/MassiveDelete")]
    [HttpDelete]
    public IHttpActionResult MassiveDelete([FromBody] int[] ids)
    {
        try
        {
            documentCtrl = documentCtrl ?? new DocumentCtrl();
            return Ok(documentCtrl.MassiveDelete(ids));
        }
        catch (DocumentNotFoundException)
        {
            return NotFound();
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

I don't have acces, any ideas what could it be?

This is my request code:

DeleteDocument(id: number): Observable<boolean> {
    return this._httpService.delete(AppModule.service + 'Document/' + id, AppModule.options)
        .map((response: Response) => <boolean>response.json())
        .catch(this.handleError);
}//This work if i want to delete one

DeleteDocuments2(ids:Array<number>):Observable<boolean>{
    AppModule.options.body=ids;
    return this._httpService.delete(AppModule.service + 'Document/MassiveDelete', AppModule.options)
        .map((response: Response) => <boolean>response.json())
        .catch(this.handleError);
}

回答1:


You cannot send two parameters in your Api, you need to createa custom class like follow and send as follows,

MyCustomRequest {
   public int[] documentIds;
   public int documentId;
}

and then,

public IHttpActionResult MassiveDelete([FromBody] MyCustomRequest  request)

you can access it as,

request.documentIds;
request.documentId;


来源:https://stackoverflow.com/questions/50893637/cant-access-to-my-method-delete-of-my-api-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!