0

I am attempting to delete a row by using the id on the row. The page will load but in dev tools it is telling me that the delete method is not allowed.

I have tried - adding the the method delete and removing it - adding and removing headers

so far nothing has worked

 handleDelete = patientId => {
  fetch("https://localhost:5001/api/PtSearchPg/delete" + patientId, {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ patientId })
  })
};

This is my table with the delete button on it

        <tbody>
          {this.state.patients.map(patient => (
            <tr key={patient.patientId}>
              <td>{patient.firstName}</td>
              <td>{patient.lastName}</td>
              <td>{patient.dob}</td>
              <td>{patient.unitId}</td>
              <td align="center">
                <button
                  type="button"
                  className="btn btn-sm btn-warning btnspace"
                >
                  Edit
                </button>
                <button
                  id="delete"
                  type="button"
                  className="btn btn-sm btn-danger"
                  onClick={this.handleDelete(patient.patientId)}
                >
                  Delete
                </button>
              </td>
            </tr>
          ))}
        </tbody>

This is the API

// DELETE api/patients/5
[HttpDelete("/delete/{id}")]
public void Delete(int id)
{
  using (var DB = new AeffectContext())
  {
    Patient deletePt = new Patient() { PatientId = id };
    DB.Patient.Attach(deletePt);
    DB.Patient.Remove(deletePt);
    DB.SaveChanges();
  }
}

So I should be deleting the patient on the row based on their id, but right now all I am getting is the 405 error.

I am new to coding so I'm sure you'll find other things I'm doing wrong haha.

  • Are you hosting in IIS? – mjwills Jul 14 '19 at 23:36
  • What folder are you looking in for your web.config? – mjwills Jul 14 '19 at 23:36
  • Please search (Control-Shift-F) for this exact string - ``. When searching, ensure that `Look in` is set to `Entire Solution ( Including External Items )` and `Look at these file types` is set to `*.*`. What files show up in the `Find Results 1` or `Find Results 2` window? – mjwills Jul 14 '19 at 23:39

2 Answers2

1

Firstly, it's highly unlikely that you use ASP.NET and don't have a web.config file, so, as the comments suggets, try searching the solution folder for it.

Also, IIS has its own configuration file (%windir%\System32\inetsrv\config\applicationHost.config) and individual HTTP verbs can be enabled/disabled there.

If you have WebDAV installed, it blocks PUT and DELETE requests by default (described here and in a dozen of similar questions - it happens to a lot of people). You can go to Turn Windows features on or off > IIS > World Wide Web Services > Common HTTP features (assuming you are using Win10) and remove WebDAV publisher from there.

Hope this helps!

Darth Veyda
  • 588
  • 2
  • 7
  • 19
  • Thanks for this help. I did a search and I can't find a web.config anywhere. I searched my api and the app. I am using react for the front-end if that makes any difference. I tried the work around that one guy posted by I just get the 400 error. – Joshua Dickson Jul 22 '19 at 15:38
0

The "delete" verb does not mean delete from a data perspective. Just use a regular "post" or "get", and handle the delete on your controller:

handleDelete = patientId => {
  fetch("https://localhost:5001/api/PtSearchPg/delete" + patientId, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ patientId })
  })
};
Programnik
  • 1,269
  • 8
  • 12
  • `The "delete" verb does not mean delete from a data perspective` https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE _sounds_ like it means delete from a data perspective. I mean, your idea **would work**. But it is a workaround at best. – mjwills Jul 14 '19 at 23:48