DELETE not working in API REST Controller DotNet Core

DELETE not working in API REST Controller DotNet Core

DiabloRitoDiabloRito Posts: 11Questions: 3Answers: 1

Hello,

Create and Edit buttons are working, but Delete button simply doesn't do anything.
I found some issues on the website with the same problem but surely not the same cause.

my code is quite simple taken from the dotnet demo.

html :

<div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-body" style="overflow-x:auto;">
                        <table id="articles" class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>Article</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>

javascript:


function articles_create_editor() { var editor = new $.fn.dataTable.Editor({ "ajax": { "create": { type: 'POST', url: get_server_api() + "articles" + '/api/rest/create/' }, "edit": { type: 'PUT', url: get_server_api() + "articles" + '/api/rest/edit/' }, "remove": { type: 'DELETE', url: get_server_api() + "articles" + '/api/rest/remove/' } }, "table": "#articles", "fields": [{ "label": "article:", "name": "article" } ] }); return editor; } function articles_create_table(editor) { var table = $('#articles').DataTable({ dom: "Bfrtip", ajax: get_server_api() + "articles" + '/api/rest/get', serverSide: true, columns: [ { data: "article" } ], select: true, buttons: [ { extend: "create", editor: editor }, { extend: "edit", editor: editor }, { extend: "remove", editor: editor } ] }); return table; } $(document).ready(function () { editor = articles_create_editor(); table = articles_create_table(editor); });

C#.NET :

private dynamic Rest(HttpRequest request)
        {
            using (var db = new Database("sqlserver", Application.singleton.connection_string))
            {
                var response = new Editor(db, "articles")
                    .Model<Article>()
                    .Field(new Field("article"))
                    .Debug(true)
                    .Process(request)   
                    .Data()
                    ;

               // return response or return JsonResult(response) throws error
                var result = new JsonResult(new {
                    //cancelled = response.cancelled,
                    data = response.data,
                    debug = response.debug,
                    //draw = response.draw,
                    //error = response.error,
                    //fieldErros = response.fieldErrors,
                    files = response.files,
                    //id = response.id,
                    //meta = response.meta,
                    options = response.options,
                    //recordsFiltered = response.recordsFiltered,
                    //recordsTotal = response.recordsTotal,
                    //upload = response.upload
                });

                return result;
            }
        }

        [Route("articles/api/rest/get")]
        [HttpGet]
        public dynamic Get()
        {
            return Rest(Request);
        }

        [Route("articles/api/rest/create")]
        [HttpPost]
        public dynamic Create()
        {
            return Rest(Request);
        }

        [Route("articles/api/rest/edit")]
        [HttpPut]
        public dynamic Edit()
        {
            return Rest(Request);
        }

        [Route("articles/api/rest/remove")]
        [HttpDelete]
        public dynamic Remove()
        {
            return Rest(Request);
        }

what i have in the debug is this request :

SELECT  [id] as 'id', [article] as 'article' FROM  [articles] 

which is surprising because when i execute the code step by step it goes correctly throw the Remove method.

In the request object i can see this :
```

<?php data[row_18][DT_RowId]=row_18&data[row_18][article]=aaaaabbbbb&action=remove ``` it's exactly the request i sent using postman. when i use my web browser then '[' and ']' get encoded to %5B and %5D if i'm not mistaken. however it doesn't seem to be the issue because when using postman '[' and ']' remain but nothing changes : DELETE button still doing nothing. ?>

Any idea about that ?

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    You will probably need to set the ajax.deleteBody option. Not all HTTP servers support body parameters in DELETE requests (the HTTP spec says it is optional) - I honestly can't remember if IIS is one of those or not, but that would be the first thing to do.

    Regards,
    Allan

  • DiabloRitoDiabloRito Posts: 11Questions: 3Answers: 1

    Hello @allan,
    I tested both options "deleteBody" but nothing changed at all.
    When i see the debug field in a create or update request, i find a create or an update query.
    However for the remove request, i can see only a select request. question is why ?

  • DiabloRitoDiabloRito Posts: 11Questions: 3Answers: 1
    edited March 2022

    I found the solution using postman :
    I had to select "body" then "form-data" then set the following parameters :
    data[row_18][DT_RowId]:row_18
    data[row_18][article]:aaa
    action:remove

    So it seems "body" is not enough, because in "body" there are many options. It should be "form-data".

    Is there an equivalent way using datatables/editor to send remove request using body/form-data ? (because it's working correctly using postman, without postman the remove request is processed as a select request it seems because no parameter is binded i suppose).

  • DiabloRitoDiabloRito Posts: 11Questions: 3Answers: 1

    actually using visual studio debugger i can see that the field "form" in "Request" object is throwing an exception :
    ((Microsoft.AspNetCore.Http.DefaultHttpRequest)request).Form '((Microsoft.AspNetCore.Http.DefaultHttpRequest)request).Form' threw an exception of type 'System.InvalidOperationException'

    so data are not mapped to form data, only to query string, even if i set deleteBody to false.

    any idea to solve that ?

  • DiabloRitoDiabloRito Posts: 11Questions: 3Answers: 1

    actually deleteBody: false solved the problem. I had just to delete cache from my browser. thanks

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    No worried - good to hear that has sorted it!

    Allan

Sign In or Register to comment.